How to define a variable in VB.NET?
Dim i_can_hold_numbers As Integer
Dim i_can_hold_decimals As Decimal
Dim i_can_hold_one_character As Char
Dim i_can_hold_text As String
As you notice the first word I used Dim, it is called "Declare In Memory". It tells the compiler that I will be creating a variable i_can_hold_numbers with type Integer after compilation, the compiler designates all variables giving them addresses for them to locate easily.
The second word is our variable name. In creating variable names, you follow a certain rules that is common to most of the programming languages.The two most important from them are,
- You can't start the name with numbers , special characters and symbols
- You can't use spaces between words, use "_" instead.
It is wrong to declare variables like this:
Dim i can hold numbers As Integer
Dim 216i_can_hold_decimals As Integer
Dim @i_can_hold_numbers As Integer
Dim !i_can_hold_numbers As Integer
- This will give you an error as stated above.
Now the last would be the variable type, It tells the compiler the things it can hold. By the way, it's too length to post all variables here, so I'll just give you a link instead. You can find the list of variables here http://en.wikibooks.org/wiki/Visual_Basic_.NET/Variables
Adding values to the variable
For numbers:
variable_name = value
For strings:
variable_name = "value"
Notice that adding numeric values just only need the value itself like 23, 1000, 10.50 while strings need an opening and closing double quote that reminds the compiler that the value is in the form of a string like "I am a string".
i_can_hold_numbers = 15
i_can_hold_text = "This is a string."
To add some information about variables, it is where you store values temporarily and is often subject for checking, especially when working with the database(Intermediate level, will be posted soon), before taking some actions, it is a need to verify each controls (Textbox, Button, Listview, etc.) if the contents may contain data or codes that is harmful to the database like SQL Injections.
It is also used for the default settings of the application, its window size and more.
see http://en.wikibooks.org/wiki/Visual_Basic_.NET/Variables for other declaration and assignment of value.
Now, you learned the basics of variables, we are ready to use them in our projects in the next article. See you soon. :D
Other variable declaration types other than "Dim" also known as "Access Levels"
- Private variable_name As variable_type
- Public variable_name As variable_type
Dim - Declare variable within the procedure, function or class.
Public - This variable is accessible in all class, procedure and functions of your project.
Private - This variable is accessible within the procedure, function or class.
Using constants
Dump text
Next topic is about "Loop Statements".

