[VB.NET] If statements

Discussion in 'Code Snippets and Tutorials' started by Will, Apr 16, 2009.

  1. Will

    Will Level IV

    Joined:
    Mar 1, 2009
    Messages:
    1,067
    Likes Received:
    53
    Here's a brief guide with an example :) (Royal said this would be helpful, so I'm posting it)


    You'll be making a program using a couple basic If statements to determine if Goku's power level is, indeed, over 9000. Yes, I know - a shocking meme - but it was all I could think of as an example :p

    Instructions:

    1. Open up VB.NET, and create a new project.

    2. In design view, create two (2) buttons, and a text box.
    Label them as follows:

    TextBox1:
    - Name: txtValue
    - Location: 13,12
    - Size: 146,20

    Button1:
    - Name: btnCheckNumbers
    - Location: 12,38
    - Size: 147,23

    Button2:
    - Name: btnExit
    - Location: 59,81
    - Size: 100,29

    3. Double click on btnCheckNumbers and paste this coding:
    Code (Text):
    1. Private Sub btnCheckNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckNumbers.Click
    2.  
    3.         'Declare variable
    4.  
    5.         Dim intNumber As Integer
    6.  
    7.         Try
    8.  
    9.             'Get the number from the text box
    10.  
    11.             intNumber = txtValue.Text
    12.  
    13.         Catch
    14.  
    15.         End Try
    16.  
    17.         'Is intNumber less than or equal to 9000?
    18.  
    19.         If intNumber <= 9000 Then
    20.  
    21.             MessageBox.Show("LESS THAN OR EQUAL TO 9000!? Yes!", "OVER 9000?! (If Demo)")
    22.  
    23.         Else
    24.  
    25.             MessageBox.Show("LESS THAN OR EQUAL TO 9000!? No!", "OVER 9000?! (If Demo)")
    26.  
    27.         End If
    28.  
    29.         'Is intNumber greater than 9000?
    30.  
    31.         If intNumber > 9000 Then
    32.  
    33.             MessageBox.Show("OVER 9000!? SRS??", "OVER 9000?! (If Demo)")
    34.  
    35.         Else
    36.  
    37.             MessageBox.Show("OVER 9000!? No!", "OVER 9000?! (If Demo)")
    38.  
    39.         End If
    40.  
    41.         'Is intNumber greater than or equal to 9000?
    42.  
    43.         If intNumber >= 9000 Then
    44.  
    45.             MessageBox.Show("OVER OR EQUAL TO 9000!? Yes!", "OVER 9000?! (If Demo)")
    46.  
    47.         Else
    48.  
    49.             MessageBox.Show("OVER OR EQUAL TO 9000!? No!", "OVER 9000?! (If Demo)")
    50.  
    51.         End If
    52.  
    53.     End Sub
    This will declare the variable and compare the value in txtValue.Text with the criteria (whether or not it is over 9000!) and return results accordingly.

    The MessageBox.Show format, if you can't already tell from the coding follows this basic format:
    Code (Text):
    1. MessageBox.Show("What you want to say in the message box", "What you want the title of the message box to be")
    and the If statement, again if you couldn't tell, follows this basic format:
    Code (Text):
    1. If [variable] <=> [constant] Then
    2. [Action if true]
    3. Else
    4. [Action if not true]
    5. End If
    6.  
    4. Double click on btnExit and paste this coding:
    Code (Text):
    1. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    2.  
    3.         Me.Close()
    4.  
    5.     End Sub
    6. End Class
    7.  
    5. Have a test of the program you just made. Enter some numbers (eg. 9000, 8999, 9001, etc) and watch the If statement at work.


    That's all there is to basic If statements in VB.NET. More advanced If statements only really build on this, and use more advanced variables.

    Hoped this helped :)