Learning VB.NET: Working with Variables, Conditionals, and Console Input - Conditionals
(Page 3 of 4 )
There are two types of conditionals in Visual Basic. The first type of conditional is the If ...Then ...Else statement, and the second type of conditional is the Select ...Case statement. Let's take a look at the If ...Then ...Else statement first. We'll create a short code snippet that accepts keyboard input and then checks to see if the user has pressed the F1 key. If the user has, then a message is displayed.
Dim input As ConsoleKeyInfo = Console.ReadKey()
If input.Key = ConsoleKey.F1 Then
Console.WriteLine("You pressed F1.")
End If
We check to see if the Key property of our ConsoleKeyInfo object is equal to the F1 value of the ConsoleKey enumeration. One thing worth mentioning is the equivalence operator in Visual Basic. Instead of using two equals signs, as in other languages, only one is used. Anyway, back to the code. If the user presses the F1 key, then a message is displayed. Otherwise, nothing is done.
Let's say, though, that in addition to displaying a message when the F1 key is pressed, we want to display a message if another key is pressed instead. To do this, we need to add an Else block:
Dim input As ConsoleKeyInfo = Console.ReadKey()
If input.Key = ConsoleKey.F1 Then
Console.WriteLine("You pressed F1.")
Else
Console.WriteLine("You pressed another key besides F1.")
End If
Now let's say that we want to display a special message for F2 in addition to a special message for F1. To do this, we need to add an ElseIf block, which is similar in structure to If:
Dim input As ConsoleKeyInfo = Console.ReadKey()
If input.Key = ConsoleKey.F1 Then
Console.WriteLine("You pressed F1.")
ElseIf input.Key = ConsoleKey.F2 Then
Console.WriteLine("You pressed F2.")
Else
Console.WriteLine("You pressed another key besides F1 or F2.")
End If
If ...Then ...Else works for complex situations, such as when the expressions are very different, or for situations where not many expressions need to be tested. Let's say, however, that we need to check for multiple keys. We could keep adding ElseIf statements, and our program would work fine. However, things would get messy and redundant. A better method would be to use a Select ...Case statement, which compares one value to numerous other values. So, with our example, we'd be comparing the Key property to values in the ConsoleKey enumeration. Let's convert the above code to a Select ...Case statement.
Dim input As ConsoleKeyInfo = Console.ReadKey()
Select Case input.Key
Case ConsoleKey.F1
Console.WriteLine("You pressed F1.")
Case ConsoleKey.F2
Console.WriteLine("You pressed F2.")
Case Else
Console.WriteLine("You pressed another key besides F1 or F2.")
End Select
In the first line of our Select ...Case, we specify our test expression. We then match the result of this expression with other values. Here, we match Key with possible values in the ConsoleKey enumeration. The result, in any case, is the same as before, except the code is a bit neater, and it would be neater still in comparison if we were to test for more keys. Needless to say, we'll be using Select ...Case when dealing with key presses in our game.
Next: Back to Console Input >>
More Visual Basic.NET Articles
More By Peyton McCullough