Variables - Declaration and Assignment
Part:
1
2
(Continued from previous part...)
c = 31
i = 777
l = 777777
f = 3.14159E+27
d = 3.33333E+202
y = 999999.5555
s = "Hello"
b = True
t = #12/31/1999 11:30:30 PM#
v = "Variant: Any data type."
MsgBox ( _
c & vbCrLf & _
i & vbCrLf & _
l & vbCrLf & _
f & vbCrLf & _
d & vbCrLf & _
y & vbCrLf & _
s & vbCrLf & _
b & vbCrLf & _
t & vbCrLf & _
v & vbCrLf)
End Sub
Run the above code, you will get a message box with the following output:
31
777
777777
3.14159E+27
3.33333E+202
999999.5555
Hello
True
12/31/1999 11:30:30 PM
Variant: Any data type.
No surprises in the output. But I haved used:
- A VB built-in constant, "vbCrLf", which represents special characters, "Carriage Return" and "Line Feed",
to break the output message into multiple lines.
- "_" character to break a VB statement into multiple lines.
Variable Default Values
If you are wondering what are the default values of explicitly declared variables of different types,
you need to see the output of the following VB code:
Sub Main()
' DefaultValue.bas
' Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
Dim c As Byte
Dim i As Integer
Dim l As Long
Dim f As Single
Dim d As Double
Dim y As Currency
Dim s As String
Dim b As Boolean
Dim t As Date
Dim v As Variant
MsgBox ( _
c & vbCrLf & _
i & vbCrLf & _
l & vbCrLf & _
f & vbCrLf & _
d & vbCrLf & _
y & vbCrLf & _
s & vbCrLf & _
b & vbCrLf & _
t & vbCrLf & _
v & vbCrLf)
End Sub
Here is the output:
0
0
0
0
0
0
False
12:00:00 AM
Note that:
- The default value for "Byte" or any numeric variable type is 0.
- The default value for "Boolean" is False.
- The default value for "Date" is "12:00:00 AM". This is interesting. Where is the date?
- The default value for "String" or "Variant" is an empty string "".
Conclusions
- Two ways to declare variables: implicit and explicit.
- VB scripts only support "Variant" variables.
Part:
1
2
|