VB.net Variables
A VB.net variables is nothing more than a name for a storage space that our programs can access. In VB.net, each variable has a type that governs the memory size and layout, the range of values that may be stored within that memory, and the set of operations that can be applied to the variable.
A variable is used in VB.NET to store a value that can be used later in the program. We’ll learn how to declare and initialize variables in this section.
Table of contents
In order for you to test your VB.net Code provided in this lesson, you can use our VB.net Online Compiler.
What is a Variable in Visual Basic?
A variable is a short name for the value of a specific data type that is stored in computer memory. Each variable in VB.net has a specific data type that determines its size, range, and fixed memory location. In any programming language, we can use variables to execute multiple operations and alter data values.
The basic value types provided in VB.net can be categorized as:
Type | Example |
---|---|
Integral types | SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char |
Floating point types | Single and Double |
Decimal types | Decimal |
Boolean types | True or False values, as assigned |
Date types | Date |
VB.net also allows defining other value types of variable like Enum and reference types of variables like Class.
VB.net Variables Declaration
The declaration of a variable is simple that requires a variable name and data type followed by a Dim. A Dim is used in Class, Module, structure, Sub, procedure.
Syntax for variable declaration in VB.Net is:
'Syntax for variable declaration in VB.Net' [ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]] [ ReadOnly ] Dim [ WithEvents ] variablelist
1. attributelist
attributelist is a list of attributes that apply to the variable. Optional.
2. accessmodifier
accessmodifier defines the access levels of the variables, it has values as – Public, Protected, Friend, Protected Friend and Private. Optional.
3. Shared
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.
4. Shadows
Shadows indicate that the variable re-declares and hides an identically named element, or set of overloaded elements, in a base class. Optional.
5. Static
Static indicates that the variable will retain its value, even when the after termination of the procedure in which it is declared. Optional.
6. ReadOnly
ReadOnly means the variable can be read, but not written. Optional.
7. WithEvents
WithEvents specifies that the variable is used to respond to events raised by the instance assigned to the variable. Optional.
8. Variablelist
Variablelist provides the list of variables declared.
Each variable in the variable list has the following syntax and parts:
'syntax for each variable in the variable list' variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
variablename
variablename − is the name of the variable
boundslist
boundslist − optional. It provides list of bounds of each dimension of an array variable.
New
New − optional. It creates a new instance of the class when the Dim statement runs.
datatype
datatype − Required if Option Strict is On. It specifies the data type of the variable.
initializer
initializer − Optional if New is not specified. Expression that is evaluated and assigned to the variable when it is created.
Some valid variable declarations along with their definition are shown here
'variable declarations along with their definition' Dim StudentID As Integer Dim StudentName As String Dim Salary As Double Dim count1, count2 As Integer Dim status As Boolean Dim exitButton As New System.Windows.Forms.Button Dim lastTime, nextTime As Date
Variable Initialization in VB.net
Variables in VB.net are initialized (assigned a value) with an equal sign followed by a constant expression.
The general form of initialization is:
//general form of initialization variable_name = value;
For example:
'variable initialization' Dim pi As Double pi = 3.14159
You can initialize a variable at the time of declaration as follows:
'initializing the variable at the time' Dim StudentID As Integer = 100 Dim StudentName As String = "Angel Jude Suarez"
Example Code for the various VB.net Variable Types
Try the following example which makes use of various vb.net types of variable
'following example of various types of variables' Module variablesNdataypes Sub Main() Dim a As Short Dim b As Integer Dim c As Double a = 10 b = 20 c = a + b Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c) 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
Test your Code Here!
Accepting Values from User
The Console class in the System namespace provides a function ReadLine for accepting input from the user and store it into a variable.
For example:
'accepting user input' Dim message As String message = Console.ReadLine
The following example demonstrates it
Module variablesNdataypes Sub Main() Dim message As String Console.Write("Enter message: ") message = Console.ReadLine Console.WriteLine() Console.WriteLine("Your Message: {0}", message) Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following result (assume the user inputs Hello IT SOURCECODERS!)
Enter message: Hello IT SOURCECODERS!
Your Message: Hello IT SOURCECODERS!
Test your Code Here!
Lvalues and Rvalues
There are two kinds of expressions:
1. lvalue
lvalue − An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
2. rvalue
rvalue − An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.
Following is a valid statement
Dim g As Integer = 20
But following is not a valid statement and would generate compile-time error
20 = g
Summary
In This Tutorial we’ve successfully discussed about the VB.net Variables and how to declare a variables in a program, also we discuss on how to store any data type Data in a variable name.
PREVIOUS
NEXT