3 Visual Basic Fundamentals
3 Visual Basic Fundamentals
Programming
2
VALID INVALID
“A rose by any other name” ‘Down by the Seashore’
“Down By the Sea Shore” “134.24
“134.23” “She said, “Stop, thief!””
“She said, ‘stop , thief!’”
Data Types
• Byte: A data type used to hold positive integer
numbers ranging from 0 to 255.
• Integer: Range –32,768 to +32,767. Can be
identified by %.
intValue% = 1
Integer = 1000
• Long: Larger range (231). Can be identified by &.
lngValue1& = 56000
lngValue2 = -56000
Data Types
• Single Precision: Real numbers. Accuracy of 7
digits. Can be identified by !.
sngReal_Number1! = 1.23
sngReal_Number2 = -0.345
• Double Precision: Accuracy of 16 digits. Can be
identified by #.
dblValue1# = 0.435827348593
dblValue2 = 23.4782947373
Data Types
• Boolean: A data type with two possible values, True
(1) or False (0).
blnAnswer = False
• Currency: This type is used in money-related
calculations and for fixed-point calculations.
• String: Hold characters, approx 231, can be null
(empty). Can be identified by $.
strText$=”Hello.”
strEmptyText = ””
Data Types
• Date: A data type used to store dates and times as
real number.
• Object: A data type that reference any Object
reference.
• Variant: A special data type that contain numeric,
string or date as well as the special values Empty and
Null. All variables become Variant type if not declared
explicitly.
• User-defined: Any data type defines using the Type
Variable Naming Convention
• must begin with a letter
• can contain letters, numeric digits, and underscore
• can have up to 255 characters
• cannot be restricted keyword
Variable Declaration
• To declare a variable, use Dim statement
• The generic syntax for declaring a variable is:
Dim variable-name As data-type
where type-name specifies the variable’s type
(such as Integer, Long, etc.), and variable-name
specifies the variable’s name.
Dim intDays as Integer
Dim strText as String, dblAns as Double
Variable Declaration
• To declare a variable, use Dim statement
• The generic syntax for declaring a variable is:
Dim variable-name As data-type
where type-name specifies the variable’s type
(such as Integer, Long, etc.), and variable-name
specifies the variable’s name.
Dim intDays as Integer
Dim strText as String, dblAns as Double
Keywords
• Words that have predefined meaning to Visual
Basic
• Can not be used as variable names
• Example:
• Print
• Cls
• If
• While
• Dim
Concatenation
• Two string can be combined with the concatenation
operation.
• Concatenation is represented with the ampersand
(&) sign.
• Example:
strVar1 = “Hello”
strVar2 = “World”
txtBox.Text = strVar1 & strVar2
txtBox.Text = “32” & CHR(176) & “
Fahrenheit”
Using Text Boxes for Input/Output
• The contents of a text box are always a string.
• Numbers are also stored in text boxes as strings.
• Therefore, the contents of a text box should be
changed to a number before being assigned to a
numeric variable.
• Val(txtBox.Text) changes the input string into a
number.
numVar = Val (txtBox.Text)