Type Example: Integral Types Sbyte, Byte, Short, Ushort, Integer, Uinteger, Long, Ulong and Char
Type Example: Integral Types Sbyte, Byte, Short, Ushort, Integer, Uinteger, Long, Ulong and Char
Type Example: Integral Types Sbyte, Byte, Short, Ushort, Integer, Uinteger, Long, Ulong and Char
A variable is nothing but a name given to a storage area that our programs
can manipulate. Each variable in VB.Net has a specific type, which determines
the size and layout of the variable's memory; the range of values that can be
stored within that memory; and the set of operations that can be applied to
the variable.
We have already discussed various data types. The basic value types provided
in VB.Net can be categorized as −
Type Example
VB.Net also allows defining other value types of variable like Enum and
reference types of variables like Class. We will discuss date types and Classes
in subsequent chapters.
Where,
Shared declares a shared variable, which is not associated with any specific
instance of a class or structure, rather available to all the instances of the class
or structure. Optional.
Shadows indicate that the variable re-declares and hides an identically named
element, or set of overloaded elements, in a base class. Optional.
Static indicates that the variable will retain its value, even when the after
termination of the procedure in which it is declared. Optional.
ReadOnly means the variable can be read, but not written. Optional.
WithEvents specifies that the variable is used to respond to events raised by the
instance assigned to the variable. Optional.
Each variable in the variable list has the following syntax and parts −
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
Where,
New − optional. It creates a new instance of the class when the Dim statement
runs.
datatype − Required if Option Strict is On. It specifies the data type of the
variable.
for example,
Dim pi As Double
pi = 3.14159
Example
Try the following example which makes use of various types of variables −
Live Demo
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c = a + b
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following
result −
a = 10, b = 20, c = 30
message = Console.ReadLine
Module variablesNdataypes
Sub Main()
message = Console.ReadLine
Console.WriteLine()
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following
result (assume the user inputs Hello World) −
Enter message: Hello World
Your Message: Hello World
rvalue − An expression that is an rvalue may appear on the right- but not left-
hand side of an assignment.
But following is not a valid statement and would generate compile-time error
−
20 = g