Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms


.

Home to new Visual Basic .NET programmers

Variables

 

Home ] What is this site? ] myExercises ] myTutorials ] myExam ] myMicrosoft ] myNotes ] Study Advice ] myLinks ]


Search (my)Visual Basic .NET

(Tip: Scroll past sponsored links to see your search results for this website)


Complain to Volkswagen South Africa (VWSA)


New to myExercises:

 

Lynnwood Consultants Program
Computer Calculator Program
Funny Time Program
Creative Minds Login Program
Hatfield Pizza & Pasta Program
  Shutterbug Program

New to myNotes:

 

>>

TOE chart


Order this book today from

kalahari.net: click-click, ding-dong

 

Title: Microsoft Visual Basic 2005:

         RELOADED, Second Edition

Author:  Diane Zak

 

Click the image below to order your copy.

 

Up ] GUI Standards ] Access Files ] Accumulators, Counters & Averages ] Arrays ] Collections ] Constants & Enumerators ] Crystal Reports ] Data Types ] Flowchart Symbols ] Functions ] Keywords ] Methods ] Operators ] Repetition Structures ] Selection Structures ] Sub Procedures ] Toolbox Controls ] Variables ] Windows Forms ]


 

Introduction

Naming a variable

Declaring a variable

variable types

    Static variables

    Object variables

    Variables

scope

 


Introduction

 

Variables are temporary memory locations for data.  Variable values can change during runtime.  It can hold data that is not stored in a control on the user interface.  It can hold data that is contained in a control's property; a computer can process data stored in a variable much faster than it can process data stored in the property of a control.  Variables can only store one piece of data at a time.  If you assign a new piece of data, the existing data in the variable will be replaced.

 

Using variables control the preciseness of data, since each variable is assigned a data type.  The data type specifies what type of data a variable can hold, e.g. string, integer, etc.

 

If you don't assign a data type to a variable, it will be assigned the Object data type by default.  The Object data type is the most flexible, as it caters for any kind of data, but it is less efficient.  It uses more memory and your program will run slower because the computer has to work out what kind of data is stored in the variable.

 

Each data type is a class.  A variable is instantiated (copied) from that class.

 

Back to Top

 

Naming a variable

 

Variables are given a three character ID.  This means of identification ensures that you know what data type each variable can hold when reading your code.  It is an industry standard, and therefore a good habit.  

 

When you thus declare a variable, attach the data type ID, e.g. strSurname or sngSales.

 

This list gives you the different data types and its three character ID

 

Data Type ID
.
Boolean bln
Byte byt
Char cha
Date dtm
Decimal dec
Double dbl
Integer int
Long lng
Object obj
Short shr
Single sng
String str

 

Rules for naming variables

 

* Give each variable a meaningful name that you will easily recognize.
* The name must begin with an alphabetical letter
* The name may only contain alphabetical letters, numbers and the underscore _
* No spaces and no punctuation is allowed
* The name you give cannot exceed 255 characters, though it is advised that no more than 32 characters be used.
* The name you give may not be a reserved word, because Visual Basic will think of it as a command, e.g. Print is a reserved word, but blnPrint will be acceptable because of the data type ID. 
* If a variable has module scope, add the letter "m" to the data type ID, e.g. mstrName, mintSales, etc.

 

Back to Top

 

Declaring a variable

 

To declare a variable means to say to Visual Basic:  "hey look, I have created a new memory location".  If you fail to delcare a variable, Visual Basic will not recognize it and therefore generate an error.

 

Syntax

 
   

accessibility VariableName [As DataType][= value]

 

  accessibility Refers to the scope of the variable, e.g. Dim, Private, Public, Static
  VariableName Is the name you give the variable, including the data type ID
  As Data type clause
  DataType The actual data type, e.g. String, Integer, etc.
  = Assignment operator
  value The value stored in the variable

 

Back to Top

 

 


All content on this site is free for private use only  |  Contributions are encouraged  |  Thank you to Netfirms for the free hosting  |  If copyright content is published here or links to certain content violates some right/law, contact the Webmaster to have it removed immediately.

 

Site launched: October 2005  |  Updated: July 20, 2006  |  Link exchange  |  Site map  |  Contact us