If…Else Statements in VB.net

By Steven Holzner Viewed: 31782 times Emailed: 201 times Printed: 219 times Bookmark and Share



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



Comments(1)


1. thank's

By: gerald at 2010-08-06 01:21:39

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2009-03-22]Handling Timer Events - and Creating an Alarm Clock in VB.net
[2009-03-22]Creating Menus in Code using VB.net
[2009-03-22]Creating Context Menus in Code using VB.net
[2009-03-22]Creating Tree Views in Code using VB.net
[2009-03-22]Creating List Views in Code using VB.net
[2009-03-16]Send SMS using VB code
[2009-02-27]Creating a Windows Service in VB.net
[2009-02-27]Creating a Windows Service Installer in VB.net
[2009-02-24]OleDbConnection class in VB.net
[2009-02-24]OleDbDataAdapter class in VB.net
[2009-02-24]DataSet Class in VB.net
[2009-02-24]DataTable Class in VB.net
[2009-02-24]DataRow Class in VB.net
[2009-02-22]Chat Server in VB.net
[2009-02-22]A tutorial on Chat Server and Chat Client in VB.net

More Latest News

Most Viewed Articles (in last 30 days)
For Loop in VB.net
Send SMS using VB code
While Loop in VB.net
Your first VB.NET Crystal Reports - A step by step guide
The Select Case statement in VB.net
Using Select Case in VB.net
Sub Procedures and Functions in VB.net
How to export from DataGridView to excel using VB.net
Arrays and Dynamic Arrays in VB.net
If…Else Statements in VB.net
Insert cell data in an Excel file using OLEDB in VB.net
Client Socket Program sample in VB.net
Handling Timer Events - and Creating an Alarm Clock in VB.net
Chat Server in VB.net
How to send email using VB.NET code
Most Emailed Articles (in last 30 days)
For Loop in VB.net
If…Else Statements in VB.net
Send SMS using VB code
While Loop in VB.net
The Option and Imports Statements in VB .NET
What's New in VB .NET? A comparison of VB vs VB.net
Visual Basic Statements
Using Select Case in VB.net
For Each…Next Loop in VB.net
The Select Case statement in VB.net
Do Loop in VB.net
“Using If with And” and Comparing two integers using If
What is .NET Framework and the Common Language Runtime?
Data types in VB.net
Arrays and Dynamic Arrays in VB.net