This is my tutorial for variables.
As always, this is for Visual Basic 6.0 and .NET
What is a variable?
-----------------------
Variable is a medium in which you can store values for read and
write procedures.
How to declare a variable:
--------------------------------
The following is the syntax for declaring variables in vb6 and
.NET:
Visual Basic 6:
Code:
Dim (variable name) [As] {variable type}
Legend:
Terms in () = Required
Terms in [] = Optional
Terms in {} = Optional but must be provided if the previous
one was provided (i.e As)
Now, to declare a variable called x you would just type Dim x.
In this way, the variable would be a VARIANT. That is, it can
take the form of ANY variable.
Visual Basic .NET:
Code:
Dim (variable name) [As] {variable type} [=] {Data}
Legend:
Terms in () = Required
Terms in [] = Optional
Terms in {} = Optional but must be provided if the previous
one was provided (i.e As, = )
The declaration of variables in .NET is next to same as VB 6.
The difference is that you can set a default value for the
variable as you declared it (like constants but changeable).
Example:
Dim FileCount As Integer = 5
Before you can understand thsese.. You'll need basic knowledge
on a few types of variables.
The ones you are going to use in your program more than any other
are:
1. Integer
2. Double
3. String
4. Long
5. Boolean
Integer:
An integer is a variable which can hold any small amount of number.
For example: 10. Or 3000.
An Integer can hold up to about 33000 to -33000 (not sure).
That is not the count of the numbers.
It is the highest value it can hold.
That is, if you assign an integer a value of 40000 then that would
create and error. An overload error. In such large number cases, you
need to use Double.
Double:
A double acts same as an integer. But double's data holding capacity
is much higher than that of integer. It is recommended to use double
in calculators. As the values are frequently expressed in decimals and
are large in size.
String:
This is the most used variable in ANY project.
A string can hold messages or information to show to the user or to
use for other purposes. To make it clear, a string is like a notepad.
It can hold various things. You may write numbers or letters in it.
For example, you declare a variable called Msg. That is: Dim Msg.
Now, if you change it to: Dim Msg As String, then the variable Msg
would be able to be used for string purposes.
That is, Msg = "Hello"
Now type MsgBox Msg. And see the variable Msg shown.
Two strings can be concentrated using "&". Example:
Dim String1 As String, String2 As String
String1 = "Hello"
String2 = "World"
MsgBox String1 & " " & String2
So, that's it for strings. NEVER try to do calculations using strings.
You will get type mismatch or similar error if you do.
Long:
Same as double and integer but can hold larger amount of numbers than
double.
This is mainly used for API call purposes..
Boolean:
Booleans are a special kind of variable which can store data in True
or False way. That is a variable called NameExists which is declared
as a boolean can hold either True or False in it. Which can be Read
or written later.
That is:
Code:
Dim NameExists As Boolean
If txtName.Text = "" Then
NameExists = False
Else:
NameExists = True
End If
I hope you got that.
That's it for variables.
There are tons of more variables out there for you to use..
Experiment with it..
-BladeX