locked
'End If' must be preceded by a matching 'If' error

    Question

  • I'm very new to VB and using VB 2010 Express for my college class and am working on my third program.  I've tried this several ways and still keep getting the same 'End If' must be preceded by a matching 'If' error.  The program is supposed to evaluate 3 numbers that are input, sort and display only the highest and lowest number in its respective box.  Although a tad lengthy, I attempted to shorten it down but still get the same errors.  I understand that there will be several ways in going about getting this result, but this is the only way I am familiar with as we are only on Chapter 3 :)  Here is the code I am using:

    Public Class Mod3Q12
       ' Comparing list of numbers for the highest and lowest.

       Private Sub compareButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compareButton.Click

          Dim number1 As Integer
          Dim number2 As Integer
          Dim number3 As Integer

          number1 = TextBox1.Text
          number2 = TextBox2.Text
          number3 = TextBox3.Text

          If number1 > number2 And number1 > number3 Then largestresult.Text(number1)
             End If

          If number2 > number1 And number2 > number3 Then largestresult.Text(number2)
             End If

          If number3 > number2 And number3 > number1 Then largestresult.Text(number3)
             End If

          If number1 < number2 And number1 < number3 Then smallestresult.Text(number1)
             End If

          If number2 < number1 And number2 < number3 Then smallestresult.Text(number2)
             End If

          If number3 < number2 And number3 < number1 Then smallestresult.Text(number3)
             End if

       End Sub
    End Class

    Although this is revised with the And statement, I initially broke down each If/Then statement and had upwards of 24 error messages,  12 for the statement after the Then which should point the output to the appropriate text box and the End If.

    Thank you for any help and advise you can give on this.

    Brian

    Monday, December 13, 2010 7:35 AM

Answers

  • Tony and Hannes,

     

    I would like to thank you both for your suggestions.  I used both, initially starting with Hannes suggestion then referring back to Tony's after getting fewer but some of the same errors.  It turned out that the code should have looked like this:

    If number1 > number2 And number1 > number3 Then
             large.Text = (number1)
          End If

    The large/small.Text = (number1/2/3) were the items that was giving me the most trouble because I checked and rechecked the spelling and such, low and behold it was the syntax and manner it was to be typed that was giving me the problem.  Once that was corrected, the 'End If' fell right back into place. 

    Again, thank you both very much!!

    Brian

    Monday, December 13, 2010 8:33 AM

All replies

  • Hi Brian,

    you can use the if statement with different style:

    If condition=true then statement

    or

    if condition=true then
        statement
        statement
    end if

    you just have mixed this. put the statement after the "then" to the next line and your error is gone.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Monday, December 13, 2010 7:52 AM
  • I don't know for sure in VB, but the syntax of an IF statement is:
     
    Either ...
     
        If condition THEN [end of line]
            [conditional code]
        Else/EndIf
        ...
     
    Or ...
     
        If condition THEN [conditional code]
     
    The way you have coded your conditions is in the second format, and the compiler doesn't understand the EndIf that follows.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, December 13, 2010 7:52 AM
  • ' Multiple-line syntax:
    If condition [ Then ]
        [ statements ]
    [ ElseIf elseifcondition [ Then ]
        [ elseifstatements ] ]
    [ Else
        [ elsestatements ] ]
    End If

    ' Single-line syntax:
    If condition Then [ statements ] [ Else [ elsestatements ] ]

    Your code is "Single-line" style but you put "End if" like "Multiple-line" style that why it's error

    I suggest you to read library in msdn topic Visual Basic Language Reference , it's help you speed up your study very much (I read all topic and help me learn 2 year knowladge in 3 month).

    Monday, December 13, 2010 8:23 AM
  • If you don't extend the statement over multiple lines, you don't need the End If

          If number1 > number2 And number1 > number3 Then largestresult.Text = number1.ToString
          If number2 > number1 And number2 > number3 Then largestresult.Text = number2.ToString
          If number3 > number2 And number3 > number1 Then largestresult.Text = number3.ToString
          If number1 < number2 And number1 < number3 Then smallestresult.Text = number1.ToString
          If number2 < number1 And number2 < number3 Then smallestresult.Text = number2.ToString
          If number3 < number2 And number3 < number1 Then smallestresult.Text = number3.ToString

     

    Monday, December 13, 2010 8:31 AM
  • Tony and Hannes,

     

    I would like to thank you both for your suggestions.  I used both, initially starting with Hannes suggestion then referring back to Tony's after getting fewer but some of the same errors.  It turned out that the code should have looked like this:

    If number1 > number2 And number1 > number3 Then
             large.Text = (number1)
          End If

    The large/small.Text = (number1/2/3) were the items that was giving me the most trouble because I checked and rechecked the spelling and such, low and behold it was the syntax and manner it was to be typed that was giving me the problem.  Once that was corrected, the 'End If' fell right back into place. 

    Again, thank you both very much!!

    Brian

    Monday, December 13, 2010 8:33 AM