Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Variables in Visual Basic (http://www.go4expert.com/articles/variables-visual-basic-t3626/)

Sanskruti 26Mar2007 19:28

Variables in Visual Basic
 
A variable is temporary storage space for numbers, text, and objects. Variables are constantly being created and destroyed and will not hold any values after your program has ended. If you want to save the values of variables or other data. Before allocating storage space for a variable, decide what the variable's lifetime will be, or in other words, which procedures and which modules should have access to the variable's value.
  • Procedure-level variables are created with a Dim statement placed right in the procedure where it's going to be used. The value of a procedure level variable cannot be accessed outside it's procedure. When the procedure finishes (End Sub or End Function), the variable is destroyed and memory allocated to the variable is released.
  • Module-level variables are created with a Private statement in the general declarations section of a Form or code module. The value of the module level variable is available to every procedure in that module. Memory allocated to the module-level variable is not destroyed until the module is Unloaded.
  • Global variables are created with a Public statement in the general declarations section of a Form or code module. The value of a Global variable is available to any procedure, in any Form or code module. Memory allocated to a Global variable is not released until your program shuts down.
It would certainly be easier to make every variable Global. You wouldn't have to think twice about it's availability at any given time. But sometimes, every byte of memory counts, so don't give your variables any more life than they actually require.

Declaring Variables



There are two principle ways to add variables to your applications.

The first method Ė called implicit declaration Ė is to let Visual Basic automatically create the variable for you. Implicit declaration means that Visual Basic automatically creates a variant for each identifier it recognizes as a variable in an application.

The second approach to declaring variable is to explicitly declare them with one of the following keywords: Dim, Static, Private, and Public. The choice of keyword has a profound effect on the variableís scope within the application and determines where the variable can be used in the program.

The syntax for explicitly declaring a variable is quite simple:

Code: VB

Dim VariableName As DataType
Static VariableName As DataType
Private VariableName As DataType
Public VariableName As DataType

In each case, the name of the variable and its data type are provided as part of the declaration.

Visual Basic reserves the amount of memory required to hold the variable as soon as the declaration statement is executed. After a variable is declared, it is not possible to change its data type, although it is quite easy to convert the value of a variable and assign the converted value to another variable.

Comparing Implicit and Explicit Variable Performance



The default data type for Visual Basic variables is the variant. This means that, unless you specify otherwise, every variable in your application will be a variant. The data type is not very efficient. Its data storage requirements are greater than the equivalent simple data type. The computer spends more time keeping track of the data type contained in a variant than for other data types.

Providing Names for Your Variables



Visual Basic provides extremely liberal rules for naming variables. Briefly, all variable names must conform to the following requirements:
  • The name must being with a letter of the alphabet.
  • The name must consist only of letters, digits, and the underscore character. (No punctuation marks are allowed.)
  • The name can be as long as 255 characters.
  • Variable names canít be duplicated with the same scope. This means, for example, that you canít have two variables of the same name within a procedure. You can, however, have two variables with the same name in two different procedures.

Static Variables



Only Procedure-level variables can be declared Static. This means the variable is not destroyed and will retain it's value between multiple calls to the procedure. This might not sound any different than a module-level variable which is used in more than one procedure while the Static variable is associated with only one.

Forcing Explicit Declaration



Visual Basic provides a simple complier directive that forces you to always declare the variables in your applications. When the Option explicit statement is inserted at the top of a module, it instructs Visual Basic to require explicit declaration of all variables in the module. If, for example, you are working with an application that contains several implicitly declared variables, inserting Option Explicit at the top of each module results in a check of all variable declarations the next time the application is complied.

Because explicit declaration is such a good idea, it might not come as a surprise that Visual Basic provides a way to automatically ensure that every module in your applications uses explicit declaration. The Editor tab of the Options dialog box includes a Require Variable Declaration check box. This option automatically inserts the Option Explicit directive at the top of very module from this point in time.

Note: The Require Variable Declaration option does not effect modules already written. This option applies only to modules created after this option is selected. Therefore, you must insert the Option Explicit directive in existing modules.

Using a Naming Convention



Like most programming languages, applications written in Visual Basic tend to be long and complex, often occupying several thousand lines of code. Even simple Visual Basic programs might require hundreds of different variables. Visual Basic forms often have dozens of different controls on them, including text boxes, command buttons, option groups, and other controls. Keeping track of the variables, procedures, forms, and controls in even a moderately complicated Visual Basic application is a daunting task.

One way to ease the burden of managing the code and objects in an application is to use naming conventions. A naming convention applies a standardized method of supplying names to the objects and variables in an application.

Using a naming convention is not difficult. In most case, when the use of the variable is obvious a one-character prefix is used to keep code examples short and simple. In longer procedures, three-character prefixes are used on most variables.

Understanding Variable Scope and Lifetime



A variable is more than just a simple data repository. Every variable is a dynamic part of the application and can be used at different times during the programís execution. The declaration of a variable establishes more than just the name and data type of the variable. Depending on the keyword used to declare the variable and the placement of the variableís declaration in the programís code, the variable might be visible to large portions of the applicationís code. Alternatively, a different placement might severely limit where the variable can be referenced in the procedures within the application.

The visibility of a variable or procedure is called its scope. A variable that can be seen and used by any procedure in the application is said to have public scope. Another variable, one that is usable by a single procedure, is said to have scope that is private to that procedure.

There are many analogies for public and private scope.Variables declared within a procedure are local to that procedure and cannot be used or referenced outside that procedure.

In each case, the Dim keyword was used to define the variable.

Dim is shorthand for dimension and is a rather archaic expression that instructs Visual Basic to allocate enough memory to contain the variable that follows the Dim keyword. Therefore, Dim i As Integer allocates less memory (2 bytes) than Dim s As Double (8 bytes). There is no way to make a variable declared within a procedure visible outside of that procedure.

The public keyword is used to make a variable visible throughout an application. Public can be used only at the module level and cannot be used within a procedure. Usually, the Public keyword is used only in standard modules that are not part of a form. Every variable declared in the general section of the standard module is public throughout the application unless the Private keyword is used. Private restricts the visibility of a variable to the module in which the variable is declared.

Steel9561 1May2008 03:05

Re: Variables in Visual Basic
 
Variables are often used in all programs and they help you store values temporarily while the program is executing. You can have long-term variables(global) which are used while the program is been used or you can have private variables which are used only on one form of the program and even local variables which are only used on a specific procedure inside a form.

Luis Lazo

vks21 25Feb2009 11:41

Re: Variables in Visual Basic
 
Variables are very useful in visual basic even every language because we can store our values in their.But before read your article means post i thought i have a proper information about variables but i was wrong now i see the different-2 ways to declare variables..its great to have you here.

Steel9561 10May2009 21:02

Re: Variables in Visual Basic
 
Quote:

Originally Posted by vks21 (Post 43428)
Variables are very useful in visual basic even every language because we can store our values in their.But before read your article means post i thought i have a proper information about variables but i was wrong now i see the different-2 ways to declare variables..its great to have you here.

Yeah, this is because variables are simply memory locations on which you store data. When we talk about variable names, we are simply talking about the name we have given to specific memory locations in order to identify the data in there.

iz_osman 1Mar2010 12:20

Re: Variables in Visual Basic
 
instead of declaring variable such:

Public fromDate as String

can i declare fromDate as follows:

Public fromDate

* without state any datatype. can it be accepted by VB 6.0

bytelogik 15May2010 21:50

Re: Variables in Visual Basic
 
"Public fromDate"
Yes, it is accepted. When no datatype is specified, it is considered as "Variant" data type by VB.

'------------------
Also in addition to VB6 specific variables, you can create your own. e.g.

Private Type Coordinate2D
X As Long
Y As Long
End Type
Dim Coordinate As Coordinate2D

Good Luck.

edwin 22Jul2010 08:45

Re: Variables in Visual Basic
 
it's really nice to read this...

ratnesh 3Oct2010 11:06

Re: Variables in Visual Basic
 
frinds i have a prblm in vb .i lost my all project when i format my comp.

MstrPBK 29May2011 08:30

Re: Variables in Visual Basic
 
Just beginning working with VB 2010, and I am puzzled as to why a variable is accepted in one place (DIM, and IF/THEN statements) and not being accepted in others (FOR/NEXT statement). I am actually dealing with a group of variables: V01 to V10 (as in V zero one to V ten). Could someone direct me as to what I might be doing wrong?

Peter Kelley
St. Paul, MN USA

mukeshsoftona 2Nov2011 15:42

Re: Variables in Visual Basic
 
Dim x
Dim carname

carname="Volvo"

Option Explicit
Dim carname
carname=some value


All times are GMT +5.5. The time now is 15:47.