0

I am writing a program that will load a text file into a textbox, find lines with specific criteria, save them to a class array called Questions with the properties(Question, CorrectAnswer, FalseAnswer1, FalseAnswer2, etc...) and then do some comparisons later.. The code WAS working, but now I am at a loss to why I receive the error:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Additional information: Conversion from string "]" to type 'Boolean' is not valid.

Sorry for the big block of code..

  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
Peter Black
  • 69
  • 1
  • 9

1 Answers1

1

I assume you mean AndAlso instead of & here:

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

Then use it

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

With & you concat strings:

Concatenation Operators in Visual Basic

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939