Variables and constants in VB.NET3.2
The concept of a variable in VB.NET is to be used to store temporary information. A constant is an idea that a piece of information will be stored throughout the running of a certain application or method.
A primitive type of variable is a type that is stored in memory as a basic structure - that is that it does not contain any information other than the value it holds. They can also be seen as basic types which are the building blocks of any programming language.
There are several primitive types in VB.NET:
- Byte is used to store a number from 0 to 255, also known as an unsigned integer.
- Short is used to store two bytes that store a signed integer.
- Integer is used to store a 4 byte signed integer, that is a 32 bit binary number.
- Long is used to store an 8 byte signed integer, that is a 64 bit binary number.
- Single is used to store a 4 byte floating point number and can store decimal numbers.
- Double is used to store an 8 byte floating point number and can store larger (twice as large as a Single) floating point numbers with decimal places
Decimal is used to store a 16 byte
number within a decimal range. It stores numbers in a format
0Dwhich represents 0.
- Decimal is used to store a 16 byte number within a decimal range. It stores numbers in a format like
- Boolean represents a 1 or a 0 in the form of True or False. It is the fundamental building block of all programming languages and computer systems.
- Date is used to store a date in the format of an American date so that the month comes first. It also stores the time as well. It looks like MM/DD/YYYY HH:MM:SS where M is the month, D is the day, Y is the year, H is the hour, M is the month, S is the second and each is a numeric value.
- Char is a character value that represent any Unicode character.
- String is an array of characters. In most programming languages, a string type is not considered primitive as it is just an array of characters.
There are hundreds more types including the Bitmap and Graphics types which can be used to store and manipulate images. These types are however not considered primitive because they are a) not building block types and b) complex structures.
A variable stores information in memory (Random Access Memory or RAM) whilst the program runs. The length of it will vary in memory based on what it contains.
The following line shows how to define a variable in VB.NET:
accessModifier name [As Type]
Type represents the type of the variable.
The  refers to an optional parameter. This means that it is not entirely necessary to be included.
The following sample shows an example of a local variable being
declared using the
Dim (Declare In Memory)
Dim i As Integer
A constant is stored permanently in memory while a class or method is running. The reason for their use is because they use the exact amount of memory that is required to store their value.
Their use throughout programming can be described as broad. For instance, retaining the colour that is used for a dialog that is called through a class or retaining the maximum number of elements in a list.
Constants are defined in a similar fashion to variables. The following line depicts how to define a constant:
Const variableName As Type
Type represents the type of variable and again,
the square brackets represents an optional parameter.
The following sample shows an example declaration of a constant in VB.NET:
'Define form dialog background colour Const backgroundColor As Color = Color.FromArgb(255, 93, 93, 44)