比较2个char值vb.net时出现未处理的异常

时间:2013-11-16 19:19:14

标签: vb.net char unhandled-exception

我正在编写一个程序,它将文本文件加载到文本框中,查找具有特定条件的行,将它们保存到名为Questions的类数组中,并使用属性(Question,CorrectAnswer,FalseAnswer1,FalseAnswer2等)和然后再进行一些比较..代码工作正常,但现在我不知道为什么会收到错误:

Microsoft.VisualBasic.dll中发生未处理的“System.InvalidCastException”类型异常 附加信息:从字符串“]”到“布尔”类型的转换无效。

很抱歉代码很大..

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim ObjectNumber As Integer
    Dim NextQuestion(CurrentTotalLines) As Questions
    Dim Q, CA, FA1, FA2, FA3, FA4, FA5 As String
    Dim Qline, QstnLine, CAline, FA1line, FA2line, FA3line, FA4line, FA5line As String
    ObjectNumber = -1

    Dim LineCounter As Integer = 0

    Dim lines() As String
    lines = TextBox1.Lines

    'For every line in my textbox...
    For Each line As String In lines
        'Lets get the first second and third letters.
        Dim firstLetter As Char
        Dim thirdLetter As Char
        Dim secondLetter As Char

        'if the lines length in letters is longer than 0
        If line.Length >= 1 Then

            'Assign the first 3 letters of the current line.
            firstLetter = line.Substring(0, 1)
            secondLetter = line.Substring(1, 2)
            thirdLetter = line.Substring(2, 3)

            'If the first and third letters are "[" & "]" We Have Found A Question!
            'and we are going to put the second letter in to our question. Which.. should
            ' be the number question we are on.
            If firstLetter = "[" & thirdLetter = "]" Then
                ObjectNumber += 1
                'Qline will be the line for the question
                Qline = LineCounter

                NextQuestion(ObjectNumber) = New Questions
                NextQuestion(ObjectNumber).QuestNumber = secondLetter
                MessageBox.Show("I found it!" & firstLetter & secondLetter & thirdLetter)


                'Queston
            ElseIf firstLetter = "Q" & secondLetter = " " & thirdLetter = " " Then
                'Hold what line we got the question from.
                QstnLine = LineCounter

                NextQuestion(ObjectNumber).Question = secondLetter


            ElseIf firstLetter = "C" & secondLetter = " " & thirdLetter = " " Then
                CAline = LineCounter
                NextQuestion(ObjectNumber).CorrectAnswer = line
            ElseIf firstLetter = "W" & secondLetter = "1" & thirdLetter = " " Then
                FA1line = LineCounter
                NextQuestion(ObjectNumber).FalseAnswer1 = line
            ElseIf firstLetter = "W" & secondLetter = "2" & thirdLetter = " " Then
                FA2line = LineCounter
                NextQuestion(ObjectNumber).FalseAnswer2 = line
            ElseIf firstLetter = "W" & secondLetter = "3" & thirdLetter = " " Then
                FA3line = LineCounter
                NextQuestion(ObjectNumber).FalseAnswer3 = line

            End If


        End If

        LineCounter += 1

    Next

1 个答案:

答案 0 :(得分:1)

我认为你的意思是AndAlso而不是&

If firstLetter = "[" & thirdLetter = "]" Then

然后使用它

If firstLetter = "[" AndAlso thirdLetter = "]" Then

使用&连接字符串:

Concatenation Operators in Visual Basic