Variables in Visual Basic
Lets start creating variables in Visual Basic. Variable is a temporary memory location stored in computer RAM which we can use for different operations. We can create many variables according to our requirements,
we can store data and retrieve data from a variable using variable name.
As we know data in variable is stored in computer RAM so its temporary data it can be used only in respective program and data will be removed automatically when a program ends.
Data in a variable can be changed during program execution any time when a change required. We can store different types of data in a variable, like numeric , text data or numbers for mathematical operations.
When we create a new variable we have to specify its name and type. For example integer variable or a string variable.
We can declare variable by selecting variable name and its type. Once a variable declared its data type cant be changed during program execution but we can change value anytime.
You can declare variables using syntax below.
DIM VariableName AS DataType DIM Value1 AS integer
Here value1 is variable name and integer its data type. In integer variable you can save numeric data type.
Variable Naming Rules
While declaring variables we can use Alphabetic and numeric values as variable name.
Here are few naming rules below.
- variable name can have maximum 255 characters.
- you cant use reserved keywords as variable names like public, private, dim etc.
- upper and lower case both allowed.
- First character must be a letter.
- you can use underscores also.
Assigning values to variables
We can assign value to a variable by using equal = operator.
Here value1 is variable name which we already declared above and = is logical operator which we used to assign 400 value to variable.