Visual Basic.NET
  Home arrow Visual Basic.NET arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 4 stars4 stars4 stars4 stars4 stars / 3
    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


    (Page 1 of 4 )

    This is the third article in a series covering how to learn VB.NET by developing a text-based game. The last article covered working with the console and how to change color schemes. This one will discuss defining variables, conditionals, and Console input. So if you're ready to keep learning, then keep reading!

    Defining Variables

    We need to cover a few more things before we can begin work on the game itself. The first is very important, and I'm afraid that perhaps I've already put it off too long. In order to make any sort of useful program, we need to be able to work with variables. Languages differ greatly in how they treat variables, and Visual Basic is no exception here. So, let's get started.

    Let's say that we want an Integer object called number. There are two things that are important here: the name of the variable (number) and the type of the variable (Integer). We need to explicitly define both when we create a new variable because Visual Basic is both strongly- and statically-typed. Here's how it's done:


    Dim number As  Integer


    The above declaration is fairly straightforward, as it should be. The thing to note here is the Dim keyword. The Dim keyword is designed to declare and allocate space for a variable. Also, note how the variable name goes before the type name, which is different from many other languages. The variable name and the type name are separated by As (after all, we're declaring number as an Integer ).

    Now that we've declared a variable, we need to assign something to it. Naturally, we'll have to assign an Integer to our variable. Assignment is done with the assignment operator and resembles assignment in other languages:


    number = 3


    Above, there are no surprises. We put the variable, the assignment operator, and then the value to be assigned to the variable.

    We can also define a variable and assign it a value in the same step. Let's combine these processes using the above example:


    Dim number As   Integer = 3


    As you can see, we simply combine the declaration step with part of the assignment step. This looks neat with the example we've chosen because we're only working with an Integer, and we're not instantiating a new object. However, let's consider a more complex object that we must explicitly instantiate. Let's create another variable containing a Date object:


    Dim anotherVariable As   Date = New   Date ()


    Aside from declaring and assigning to a variable, the above line of code does something else very important: it instantiates a Date object. The Date type is used to represent a date and time. Instantiation in Visual Basic is not wildly different from instantiation in other languages, as you can see above. We simply use the New keyword and call the appropriate constructor.

    Back to declaration and assignment, though. The above code does look a bit redundant. The type is essentially specified twice, and very close together, too. It looks a bit ugly, but, thankfully, there's another way to do this. In fact, the above method is discouraged, and I only brought it up so that you may recognize it if you ever encounter it. The other method is more concise:


    Dim anotherVariable As   New   Date ()


    This looks much neater and more elegant, and it's the style you should always use if you need to declare a variable and assign a newly instantiated object to it in one step.

    More Visual Basic.NET Articles
    More By Peyton McCullough


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

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek