By: Steven Holzner in VB.net Tutorials on 2008-11-25
How can you make choices in your code, deciding what to do next depending on the values in your variables? You can use the If statement, which is the bread-and-butter of Visual Basic conditionals, and which lets you evaluate your data and execute appropriate code. Here's how this statement works:
If condition Then [statements] [ElseIf condition-n Then [elseifstatements] -] [Else [elsestatements]] End If
You use comparison operators in the condition here to generate a logical result that's true or false; for example, if condition is intVariable > 5, then condition will be true if the value in intVariable is greater than 5.
If condition is True, the statements immediately following the Then keyword in the body of the If statement will be executed, and the If statement will terminate before the code in any ElseIf or Else statement is executed. If condition is False, the following ElseIf statements are evaluated, if there are any; this statement lets you test additional conditions, and if any are True, the corresponding code (elseifstatements above) is executed and the If statement terminates. If there are no ElseIf statements, or if none of their conditions are True, the code in the Else statement (elsestatements above), if there is one, is executed automatically.
Here's an example to show you how to use the various parts of this popular statement. In this case, I'm reading an integer that the user types at the console-using the System.Console.ReadLine method-and checking it against various values:
Module Module1 Sub Main() Dim intInput As Integer System.Console.WriteLine("Enter an integer-") intInput = Val(System.Console.ReadLine()) If intInput = 1 Then System.Console.WriteLine("Thank you.") ElseIf intInput = 2 Then System.Console.WriteLine("That's fine.") ElseIf intInput = 3 Then System.Console.WriteLine("Too big.") Else System.Console.WriteLine("Not a number I know.") End If End Sub End Module
Note that when you compare strings, not numbers, in condition, the string expressions are evaluated on the basis of their alphabetical sort order by default. The sort order for strings is evaluated based upon the Option Compare setting. You can also use the String.Compare method to compare strings.
Also note that you can also use the TypeOf and Is keywords to check the type of an object in an If statement, like this:
If (TypeOf Err.GetException() Is OverflowException) Then System.Console.WriteLine("Overflow error!") End If
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in VB.net )
Latest Articles (in VB.net)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews