Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Learning VB.NET: Working with Variables, C...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Learning VB.NET: Working with Variables, Conditionals, and Console Input
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-06-11

    Table of Contents:
  • Learning VB.NET: Working with Variables, Conditionals, and Console Input
  • Reading Console Input
  • Conditionals
  • Back to Console Input

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · Hello everyone,This is a continuation of the series introduced...
     

    VISUAL BASIC.NET ARTICLES

    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT