我的代码中导致此错误的原因是什么?

时间:2014-03-04 18:15:30

标签: vb.net

我正在为我的计算机科学课进行测验,基本概念是你有15个关键词和15个定义。所有这些都需要随机显示,并且必须出现正确的答案。用户必须将正确的定义与关键字匹配两次,然后再次不显示该关键字和定义。当所有人都被回答两次时,测验就结束了。

我已将我的关键字和我的定义存储在同一个文件中,因此它们不会失去同步。文本文件如下所示:

Keyword1,Definition1
Keyword2,Definition2
Keyword3,Definition3
...

我得到的问题是我的代码中有两行。首先,我将参考所有相关代码,使我所要求的内容尽可能清晰。我创建了一个类Answer,看起来像这样:

Public Class Answer
Public Answer As String
Public Answered As Boolean

End Class 我的主要表单form1如下所示:

Public Class Form1
Const NUMBER_OF_ANSWERS As Integer = 3

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    kv = New Dictionary(Of String, Answer)
    For Each line As String In IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt")
        Dim parts() As String = line.Split(",")
        kv.Add(parts(0), New Answer With {.Answer = parts(1), .Answered = False})
    Next

    Dim r As New Random
    Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
      kv.OrderBy(Function() r.Next).ToList

    'questions will appear in random order
    For Each line As KeyValuePair(Of String, String) In kvRandom
        Dim keyword As String = line.Key
        Dim correctDefinition As String = line.Value

        Dim keywords As New List(Of String)
        keywords.Add(keyword)
        keywords.AddRange(kv.Keys.Except({keyword}).
          OrderBy(Function() r.Next).Take(NUMBER_OF_ANSWERS - 1))

        Dim definitionsRandom As List(Of String) =
          keywords.Select(Function(x) kv(x)).OrderBy(Function() r.Next).ToList

        LabelKeyword.Text = keyword
        RadioButtonDef1.Text = definitionsRandom(0)
        RadioButtonDef2.Text = definitionsRandom(1)
        RadioButtonDef3.Text = definitionsRandom(2)
    Next

End Sub

我的错误在以下两行:

kv.OrderBy(Function() r.Next).ToList即:

Error 1 Value of type 'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, flashquiz.Answer))' cannot be converted to 'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, String))'. C:\Users\Matt\Desktop\flashquiz\flashquiz\Form1.vb 62 11 flashquiz

keywords.Select(Function(x) kv(x)).OrderBy(Function() r.Next).ToList即:

Error 2 Value of type 'System.Collections.Generic.List(Of flashquiz.Answer)' cannot be converted to 'System.Collections.Generic.List(Of String)'. C:\Users\Matt\Desktop\flashquiz\flashquiz\Form1.vb 75 15 flashquiz

我不确定有什么问题,所以任何帮助都将不胜感激。 我知道这与Answer类有关,但不完全是什么。

1 个答案:

答案 0 :(得分:4)

第一个违规行在这里:

Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
  kv.OrderBy(Function() r.Next).ToList

在此行中,您将获取kv中的所有项目,随机对其进行排序,并将其作为List(Of Answer)返回。然后,您尝试将该列表分配给声明为kvRandom的{​​{1}}变量。由于这两种类型不兼容,它会给你错误。

要解决此问题,您需要将List(Of KeyValuePair(Of String, String))更改为kvRandom,如下所示:

List(Of Answer)

或者,您需要添加Dim kvRandom As List(Of Answer) = kv.OrderBy(Function() r.Next).ToList() 方法才能正确转换列表中的项目,例如:

Select

第二个违规行在这里:

Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
    kv.OrderBy(Function() r.Next) _
    .Select(Function(x) New KeyValuePair(Of String, String)(x.Key, x.Value.Answer)) _
    .ToList()

Dim definitionsRandom As List(Of String) = keywords.Select(Function(x) kv(x)).OrderBy(Function() r.Next).ToList 方法返回lambda表达式Select返回的项列表。由于Function(x) kv(x)kv,这意味着Dictionary(Of String, Answer)将返回kv(x)个对象。因此,Answer方法返回Select个对象的列表。然后,您将按随机顺序对其进行排序,并将其转换为Answer对象。然后,您尝试将其分配给List(Of Answer)变量,即definitionsRandom。由于它们是两种不同的不兼容类型,因此它会给你错误。

要修复它,您需要在调用List(Of String)方法时更改lambda表达式,以返回Select而不是String对象。例如:

Answer

注意,lambda表达式返回Dim definitionsRandom As List(Of String) = keywords.Select(Function(x) kv(x).Answer).OrderBy(Function() r.Next).ToList 而不是kv(x).Answer