The Assignment Statement
Format: <variable name> = <expression>
The assignment statement causes the value of the expression on the right side of the equal sign to be stored in the variable specified on the left side of the equal sign.
An expression can be a constant, variable, or any valid combination of constants and/or variables connected with the operators such as +, -, *, and /.
Example 1
The assignment statement has the form variable = constant
(i.e., the <expression> is a constant)
If the variable name on the left of the equal sign is a string variable, then the constant must be a string constant enclosed in quotes. The quotes are not stored. Examples follow:
Assume that the following variables are declared:
Dim strCustName As String |
Dim strCustAddr As String |
The statement
strCustName = "BOB SMITH"
would cause the characters BOB SMITH to be stored in the variable strCustName.
The statement
strCustAddr = "123 MAIN ST."
would cause the characters 123 MAIN ST. to be stored in the variable strCustAddr.
You can also declare and initialize a variable in one line:
Dim strCustName As String = "BOB SMITH" |
Dim strCustAddr As String = "123 Main ST." |
I will recommend declaring and initializing the variables in different lines.
If the variable name on the left of the equal sign is a numeric variable (Integer, Long, Single, Double,Decimal), then the constant must be a valid numeric constant. Numeric constants must not be enclosed in quotes. They must consist only of the digits 0 through 9 and can optionally have a leading sign (- or +) and may have one decimal point. Examples:
Assume that the following variables are declared:
Dim intPageLimit As Integer |
Dim lngBigInt As Long |
Dim sngHrlyRate As Single |
Dim dblCreditAmt As Double |
The following statements would cause the specified quantities to be stored in these variables:
intPageLimit = 60 |
lngBigInt = 40567 |
sngHrlyRate = 12.50 |
dblCreditAmt = -1527483.98 |
Date and time values are stored internally in a special format, but you don’t need to know the exact format. They are double precision numbers: the integer part represents date and the fraction part represents the time. Assume that the following variable is declared:
Dim dtmHireDate As Date
The following statement would cause the internal representation of November 29, 1999 to be stored in the variable dtmHireDate:
dtmHireDate = #11/29/99#
The statements given below are also the valid statements
dtmHireDate = #11/29/99 6:30:11 PM#
dtmHireDate = “November 2, 1999”
dtmHireDate = Now( )
Note: Whenever we assign a value to a variable name, the previous value stored in that variable is destroyed and replaced with the new value. This is called a "destructive replacement" or "destructive write". For example, if the previous value of the variable intI was 2, and then the statement intI = 6 was executed, the new value of intI would be 6 (the 6 would replace the 2).
Example 2
The assignment statement has the form variable = variable
(i.e., the <expression> is a variable)
The contents of the variable on the right of the equal sign will be copied to the variable on the left of the equal sign, replacing the previous contents of the variable on the left. The contents of the variable on the right will remain unchanged.
The examples below assume that the following variables have been declared:
Dim strName1 As String |
Dim strName2 As String |
Dim intI As Integer |
Dim intJ As Integer |
Contents before assignment statement is executed: |
strName1 |
strName2 |
|
JOHN |
BILL |
|
|
|
Statement strName1 = strName2 is executed |
|
|
|
|
|
Contents after assignment statement is executed: |
strName1 |
strName2 |
|
BILL |
BILL |
Contents before assignment statement is executed: |
intI |
intJ |
|
5 |
10 |
|
|
|
Statement intI = intJ is executed |
|
|
|
|
|
Contents after assignment statement is executed: |
intI |
intJ |
|
10 |
10 |
|
|
|
Example 3
The assignment statement has the form variable = expression
(i.e., the <expression> is an arithmetic expression)
If an arithmetic expression is on the right of the equal sign, the expression is evaluated and the result of the expression is stored in the variable on the left of the equal sign.
For example, given the two statements:
intI = 2 |
intJ = intI + 4 |
In the second statement above, VB .NET will determine that the variable intI contains 2, add the constant 4 to it, and store the result (6) in intJ.
Given these two statements:
intI = 2 |
intI = intI + 1 or intI += 1 |
In mathematics, you could never have a statement like the second one above (intI = intI + 1), because in mathematics, the "=" indicates equality. But in VB .NET, the "=" does not indicate equality; rather, it means "is replaced by" - i.e., the expression on the right (intI + 1) is first evaluated and determined to be the value 3. The 3 is then stored in the variable on the left (which happens to be intI).
Other assignment statement notes:
There can never be an expression on the left of the equal sign. For example,
A + B = C + D
is invalid, it means nothing in VB .NET. By definition, the function of the assignment statement is to store a value in the variable specified on the left of the equal sign.
In previous versions of BASIC and VB, you could only assign a string constant or another string variable to a string variable, and you could only assign a numeric constant, numeric variable, or numeric expression to a numeric variable. Violation of this rule resulted in the generation of a Type Mismatch error. In later versions of VB, this rule has been relaxed (only if Option Strict is OFF in VB .NET) – basically, VB will convert one data type to another if it possibly can; if it can't perform a suitable conversion, the "Type Mismatch " error will still occur. Generally, "mixed-mode" assignment statements should be avoided when possible; they are inefficient and may sometimes produce unexpected results.
Examples:
Statement |
Notes |
Dim strMyString As String |
|
Dim intMyInt As Integer |
|
Dim sngMySingle As Single |
|
|
|
intMyInt = 2 |
|
strMyString = intMyInt |
causes string representation of "2" to be stored |
intMyInt = "5"
|
converts the string "5" to integer representation of 5 |
intMyInt = "ABC"
|
Produces a Type Mismatch error – "ABC" can't be converted to a number |
sngMySingle = 3.9 |
|
strMyString = sngMySingle |
stores the string "3.9" in strMyString |
intMyInt = sngMySingle |
VB rounds 3.9 up and stores 4 in intMyInt |
sngMySingle = 3.5 |
|
intMyInt = sngMySingle |
VB rounds 3.5 up and stores 4 in intMyInt |
sngMySingle = 4.5 |
|
intMyInt = sngMySingle |
VB rounds 4.5 down and stores 4 in intMyInt |
Regarding the last statement, if the decimal portion of a Single or Double is exactly .5, VB always rounds to the nearest even number when converting to an Integer or Long. This is sometimes referred to as "bank rounding".
In the "mixed mode" assignment statements shown above, VB would perform the necessary conversions in these cases wherever it could. Such conversions are called implicit conversions.
In VB, you can also use a set of functions that explicitly convert (or "cast") one type of data to another. The set of functions that enable you to do this all begin with the letter "C": CBool, CByte, CChar, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, CObj, and CShort. There are also two older functions, Val and Str, which enable you to perform conversions as well. These functions will be covered in a later topic.
Assigning Data to Arrays
Given the following definitions:
Dim aintCount(9) As Integer
Dim asngSales(4,5) As Single
The following would be valid assignment statements:
aintCount(4) = 36
asngSales(2, 3) = 12543.22
You can also assign the arrays while declaring them:
Dim aintCount(3) As Integer = {0,1,2,3}
Assigning Data to Structures
Given the following definitions:
Public Structure EmployeeName
FirstName As String
MidInit As String
LastName As String
End Structure
Public Structure EmployeeRecord
udtEmpName As EmployeeName
dtmHireDate As Date
sngHourlyRate As Single
dblQuarterlyEarnings(4) As Double
End Structure
Dim udtEmpRec As EmployeeRecord
Dim audtEmpRec(10 As EmployeeRecord
The following would be valid assignment statements:
udtEmpRec.sngHourlyRate = 28.75
audtEmpRec(3).dtmHireDate = #1/15/2001#
udtEmpRec.udtEmpName.MidInit = "B"
audtEmpRec(4).dblQuarterlyEarnings(3) = 14950.00
Using With/End With
You can use a With/End With block to "factor out" a qualifying reference in a group of statements. For example, the following sets of statements are equivalent:
udtEmpRec.udtEmpName.FirstName = "JOHN" udtEmpRec.udtEmpName.MidInit = "B" udtEmpRec.udtEmpName.FirstName = "SMITH" udtEmpRec.dtmHireDate = #1/15/2001# udtEmpRec.sngHrlyRate = 28.75 udtEmpRec.dblQuarterlyEarnings(1) = 14950.00 udtEmpRec.dblQuarterlyEarnings(2) = 14950.00 udtEmpRec.dblQuarterlyEarnings(3) = 14950.00 udtEmpRec.dblQuarterlyEarnings(4) = 7475.00
|
With udtEmpRec .udtEmpName.FirstName = "JOHN" .udtEmpName.MidInit = "B" .udtEmpName.FirstName = "SMITH" .dtmHireDate = #1/15/2001# .sngHrlyRate = 28.75 .dblQuarterlyEarnings(1) = 14950.00 .dblQuarterlyEarnings(2) = 14950.00 .dblQuarterlyEarnings(3) = 14950.00 .dblQuarterlyEarnings(4) = 7475.00 End With |
With statement blocks can be nested. The following sets of statements are equivalent:
With udtEmpRec .udtEmpName.FirstName = "JOHN" .udtEmpName.MidInit = "B" .udtEmpName.FirstName = "SMITH" .dtmHireDate = #1/15/2001# .sngHrlyRate = 28.75 .dblQuarterlyEarnings(1) = 14950.00 .dblQuarterlyEarnings(2) = 14950.00 .dblQuarterlyEarnings(3) = 14950.00 .dblQuarterlyEarnings(4) = 7475.00 End With |
With udtEmpRec With .udtEmpName .FirstName = "JOHN" .MidInit = "B" .FirstName = "SMITH" End With .dtmHireDate = #1/15/2001# .sngHrlyRate = 28.75 .dblQuarterlyEarnings(1) = 14950.00 .dblQuarterlyEarnings(2) = 14950.00 .dblQuarterlyEarnings(3) = 14950.00 .dblQuarterlyEarnings(4) = 7475.00 End With |