Variables


Variables

Now that you have an idea of the types of data that Visual Basic .NET makes available to you, let's explore how you can actually use these data types in a program to get something productive done.

When you think about it, a computer program is usually designed with one thing in mind: manipulating data. When you write an email message to a friend, the program accepts the keystrokes you make, formats the text into an email message, and then passes along the message to your friend. If you run a program to figure out how much a bank loan will cost you per month, you type in the amount of the loan, the interest rate, and the number of months, and then you press the Enter key; the monthly payment is then calculated. In each of these cases, the program accepts data from you via the keyboard, processes that data, and presents new data based on what you typed in to the program.

Regardless of the exact purpose of the program, the basic sequence is the same: Get some data from the user, process that data, and then display the result to the user . The sequence can be simplified to three basic steps (as described in Chapter 3, "Thinking About Programs"):

  1. Input the data.

  2. Process the data.

  3. Output the results.

Clearly, the program needs some place to hold the data after you type it in to the program. The place where the program holds the data is called a variable.

Variable Name

You define a variable to hold a data item. You create a variable's name yourself, and there are certain rules that you must follow when you create a valid variable name:

  1. The variable name must start with a letter or an underscore character.

  2. The variable name cannot have punctuation marks or special characters in it.

  3. The variable name cannot be a Visual Basic .NET keyword. (You'll learn more about keywords in the following section.)

Consider the variable names presented in Table 4.2.

Table 4.2. Valid and Invalid Variable Names

Variable Name

Valid or Invalid

Comment

MyName

Valid

Meets all rules

Hatsize

Valid

Meets all rules

34Waist

Invalid

Violates Rule 1; starts with a number

My.Phone.Number

Invalid

Violates Rule 2; has period characters

Date

Invalid

Violates Rule 3; Date is a Visual Basic .NET keyword

Negative-Number

Invalid

Violates Rule 2; has hyphen sign character

_As

Valid

Meets all rules; note leading underscore character

  As  

Invalid

Violates Rule 3; As is a Visual Basic .NET keyword

Visual Basic .NET programmers follow some naming conventions for variables, and you'll learn when you start writing your own programs in later chapters. For now, you should know that you need to follow the three naming rules for variables.

Keywords

Rule 3 for variable naming states that you cannot use a Visual Basic .NET keyword as a variable name. A keyword is a word that has special meaning to Visual Basic .NET. A keyword causes Visual Basic .NET to perform a certain action or process associated with the keyword. You will learn what each of the Visual Basic .NET keywords mean as you progress through this book. The following are the keywords for Visual Basic .NET:

AddHandler

AddressOf

Alias

And

AndAlso

Ansi

  As  

Assembly

Auto

Boolean

ByRef

Byte

ByVal

Call

Case

Catch

CBool

CByte

CChar

CDate

CDec

CDbl

Char

CInt

Class

CLng

CObj

Const

CShort

CSng

CStr

CType

Date

Decimal

Declare

Default

Delegate

Dim

DirectCast

  Do  

Double

Each

Else

ElseIf

End

Enum

Erase

Error

Event

Exit

#ExternalSource

False

Finally

For

Friend

Function

Get

GetType

GoTo

Handles

  If  

Implements

Imports

  In  

Inherits

Integer

Interface

  Is  

Let

Lib

Like

Long

Loop

  Me  

Mod

Module

MustInherit

MustOverride

MyBase

MyClass

Namespace

New

Next

Not

Nothing

NotInheritable

NotOverridable

Object

  On  

Option

Optional

Or

OrElse

Overloads

Overridable

Overrides

ParamArray

Preserve

Private

Property

Protected

Public

RaiseEvent

ReadOnly

ReDim

#Region

REM

RemoveHandler

Resume

Return

Select

Set

Shadows

Shared

Short

Single

Static

Step

Stop

String

Structure

Sub

SyncLock

Then

Throw

  To  

True

Try

TypeOf

Unicode

Until

Variant

When

While

With

WithEvents

WriteOnly

Xor

#Const

#ExternalSource

#If...Then...#Else

#Region

   

Remember that you cannot use keywords as variable names. If you try to create a variable name by using a keyword, you get an error message from Visual Basic .NET.

The Dim Statement: Defining Variables

Before you can use a variable in a program, you need to define it. For example, suppose you want to create a variable named Age for use in a program. Further assume that you are only interested in using a person's age as an integer value. That is, you only care if a person is 37, not 37.5. To define the Age variable, you would type the following line into your program:

 Dim Age As Integer 

When you type this line and press the Enter key, Visual Basic .NET examines it. In other words, Visual Basic .NET scans , or parses, the line you typed to see if the line conforms to the rules of valid Visual Basic .NET statements. A statement is nothing more than a sequence of expressions, or words, that obey the rules of the Visual Basic .NET language. These language rules are called the syntax rules of the language. If you type something into a program that Visual Basic .NET determines does not obey the syntax rules, Visual Basic .NET issues a syntax error. Visual Basic .NET is smart enough to write a squiggly line under the word (or words) that cause a syntax error. (The squiggly line is the result of Visual Basic .NET's IntelliSense feature.) Because you do not get a syntax error when you type in the program code line

 Dim Age As Integer 

you know you have entered a statement that conforms to the Visual Basic .NET syntax rules.

The Dim keyword at the start of this statement tells Visual Basic .NET that you want to create a variable for use in the program. The word immediately following the Dim keyword is the name you want to use for the variable ( Age in this example). The last two keywords, As Integer , tell Visual Basic .NET the data type, also called the type specifier , to use for the variable named Age . Because you have obeyed all the syntax rules of Visual Basic .NET in this statement, you now have a variable named Age that you can use in your program to store integer-type data.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net