Variables, Assignment Statements, and Arithmetic
Variables, Assignment Statements, and Arithmetic
Conversion Purpose
Function
CBool Convert argument to Boolean
CCur Convert argument to Currency
CDate Convert argument to Date
CInt Convert argument to Integer
CSng Convert argument to Single
CDbl Convert argument toDouble
Assignment Statements
• Must convert strings in text boxes to numeric with
Val function, eg,
– curPrice = Val(txtPrice)
• Convert numeric variables to strings before
assigning to text box, eg,
– txtAmountDue = str(curAmountDue)
• Carry out calculations with assignment statements,
eg,
– curTaxes = 0.07*Price
– curAmountDue = curPrice + curTaxes
VB Code Box 3-2
Use Val() Function
in Inputting Numbers
Private Sub cmdSum_Click()
’Declare variables
Dim intFirst as Integer,intSecond as Integer
Dim intSum as Integer
’Assign values to variables as number
intFirst = Val(txtFirstNum.Text)
intSecond = Val(txtSecondNum.Text)
End Sub
VB Code Box 3-3
Code for cmdSum Command Button
Private Sub cmdSum_Click()
’Declare variables
Dim intFirst as Integer, intSecond as Integer
Dim intSum as Integer
’Assign values to variables as a number
intFirst = Val(txtFirstNum.Text)
intSecond = Val(txtSecondNum.Text)
intSum = intFirst + intSecond ’Calculate sum
txtSum.Text = Str(intSum) ’display sum in sum text box
End Sub
Properties Versus Methods
• The dot notation method is the way properties are set at run
time.
• Syntax: object. property= value
E.g. txtSum.Text=Str(intSum)
• The SetFocus method shifts the cursor to the named text box
• Methods can’t be used in assignment statements
VB Code Box 3-4
Code for cmdClear Command Button
Private Sub cmdClear_Click()
’Clear text boxes with empty string
txtFirstNum.Text = ""
txtSecondNum.Text = ""
txtSum.Text = ""
txtFirstNum.Setfocus ’Set focus back to first text box
End Sub
Using Assignment Statements for
Calculations
• An expression is a combination of one or
more variables and/or constants with
operators
• A constant is a quantity that does not
change
• Operators are symbols used for carrying out
processing
Arithmetic Operators ****
• () for grouping + for addition
• ^ for exponentiation - for subtraction
• - for negation * for multiplication
• / for division
• \ for integer division (divisor and dividend
are rounded to 0)
• Example 7.1111\1.95= 7\2 =3
• mod for modulus
• Example 7.1111\1.95= 7\2 =3 with 1 remainder
Hierarchy of Operations ****
• Operations within parentheses ( )
• Exponentiation (^)
• Negation (-)'
• Multiplication and division (*,/)
• Integer division (\)
• Modulo arithmetic (Mod)
• Addition and subtraction (+,-)
• String concatenation (&)
3 1 2 5 4 (order)
• Order
1 Subtract Taxes from Salary
2 Square the result
3 Multiply this result by 3
4 Divide Bonus by Months
5 Subtract result from first expression
****You will see something like this again!
String Operators
Function Description
FormatCurrency Expression formatted as currency along
with currency symbol
FormatDateTime Expression formatted as a date or time
Price = CCur(TxtVideoPrice.Text)
Taxes = Price * TaxRate ’calculate taxes
AmountDue = Price + Taxes ’calculate amount due