Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - 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 - Reading Console Input


    (Page 2 of 4 )

    So far, we've used the console for output, but our game, of course, needs to be able to accept input from the user. Now that we know how to work with variables, we can start tackling input. As with output, the Console class is used for input. With output, we started with the WriteLine procedure, and with input, we'll start with ReadLine. Whereas WriteLine writes a complete line to the console, ReadLine reads a complete line from the console (that is, the user). The input is then stored in a string. Here's ReadLine in action:


    Dim input As  String = Console.ReadLine()

    Console.WriteLine(input)


    The first line of the above snippet reads a line of input from the user, and the second line writes the line back out to the user.

    ReadLine will work fine for many purposes. For example, we might ask the user his name, which he'll type in and then press enter. However, ReadLine is not appropriate for input because not all input will be in line format. For example, when the user wants to move around, he'll press an arrow key. We'll then need to intercept the pressed key and treat it as an individual key rather than a line. For situations like this, we'll need to use the ReadKey function, which reads an individual key from the console and then returns information about the key in the form of a ConsoleKeyInfo object. Here's a basic example of ReadKey:


    Dim input As ConsoleKeyInfo = Console.ReadKey()


    Our ConsoleKeyInfo object has a number of properties that describe the key pressed. One property is the KeyChar property, which will return the character representation of the key pressed:


    Console.WriteLine("You pressed the '{0}' key.", input.KeyChar)


    However, this property has some limitations. ReadKey will not only read basic alphanumeric keys, but it will also read other keys, such as function keys, that do not have appropriate character representations. For example, try running the above two lines of code and then pressing the F1 key.

    The next property of a ConsoleKeyInfo object is Key. The Key property will return a value from the ConsoleKey enumeration. This enumeration contains values for all of the keys. So, to check which key has been pressed, one would simply need to compare the value in Key with the values in the ConsoleKey enumeration. To do this, however, we need to take a look at conditionals.

    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 2 hosted by Hostway
    Stay green...Green IT