Variables, Constants and Calculations
Variables, Constants and Calculations
Calculations
VISUAL PROGRAMMING
WEEK 2
Variables
• Provide a means to store data values that are not stored in files.
• Support the computation of values through their use in formulas in
assignment statements.
• Represent locations in a computer's memory.
• are assigned a unique name for reference when writing computer
code
Variables
Variables Data Types
• Variables of different data types exist so that you can store
different types of values.
• Integer is one of the data types available in Visual Studio .NET
to represent whole numbers.
• You must select from a list of variable data types indicating to
Visual Basic .NET how much space to allocate and how to
process and display the variable.
Variables Data Types
Which data type is chosen depends on the range of values
required by the variable.
Maximum and minimum size of a variable must be taken into
account.
Short data type is for values between –32,768 and 32,767.
Integer data type can store a value from –2,147,483,648 to
2,147,483,647.
Long can store a value from –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
Selecting Proper Data Types
Overflow is the term used to describe when you try to store a
value in a variable that is too big.
Private Sub btnCalculate_Click(…
Dim shtVariable As Short
shtVariable = 32767
shtVariable = shtVariable + 1
End Sub
Data Types Summary
Data Type Description Range
Boolean Logical data True or False
Date Date and time data January 0100, to December 31, 9999
Decimal Large floating point number Varies in size depending on the value stored; can
hold values much larger or more precise than a
Double
Double Large or high precision floating point Floating point number with up to 14 digits of
numbers accuracy
Integer Large integer number -2,147,483,648 to 2,147,483,647
Long Really large integer numbers –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
The order of precedence for expressions that have more than one operation is the same as
for other programming languages.
Evaluate values and calculation symbols in this order:
(1) Values enclosed inside parentheses
(2) Exponentiation
(3) Multiplication and Division
(4) Integer Division
(5) Modulus Division
(6) Addition and Subtraction
Assignment Operators and Formulas
The equal sign is the assignment operator. It means store the value of
the expression on the right side of the equal sign to the memory
variable named on the left side of the equal sign. Examples:
Operation Long Way of Writing the Statement Short Way of Writing the
Statement
Addition intVar = intVar + 1 intVar += 1
Subtraction intVar = intVar – 1 intVar -= 1
Division intVar = intVar / 1 intVar /= 1
Multiplication intVar = intVar * 1 intVar *= 1
String strVar = strVar & “New Text” strVar &= “New Text”
concatenation
Example: Counter Application
Create an application that acts as a counter.
A counter should start at 0 and increment by 1
each time a button is pressed.
It is also useful to have an additional button
that will reset the counter to 0.
Perform the following steps
Create Form
Step 1: Create a new project.
Step 2: Set the Name property of the form to frmCounter.
Step 3: Set the Text property to Simple Counter
Application.
Step 4: Right-click on Form1.vb in the Solution Explorer and
rename the form to frmCounter.vb.
Add intCounter Variable
Step 1: Right-click on the form.
Step 2: Click on the View Code item in the pop-up menu.
Step 3: Your code should default to the Declarations section. The
pull-downs of your code should look like this one below.
Add Title Label
Step 1: Place a label control in the middle of the
form.
Step 2: Set the Name to lblTitle.
Step 3: Set the Text to Simple Counter
Application.
Step 4: Set the Font Size property to 14 and the
Font Bold to True.
Step 5: Set the TextAlign property to
MiddleCenter.
Add Counter Label
Step 1: Place a label control in the middle of
the form.
Step 2: Set the Name to lblCounter.
Step 3: Set the Text to 0.
Step 4: Set the Font Size property to 24
and the Font Bold to True.
Step 5: Set the TextAlign property to
MiddleCenter.
Add Counter Button
Step 1: Place a button control in the lower-left side of the form.
Step 2: Set the Name to btnCounter.
Step 3: Set the Text to Counter.
Step 4: Double-click on the button.
Step 5: Attach the code to add 1 to the counter as shown below.
Add Reset Button
Step 1: Place a button control in the lower-right side of the form.
Step 2: Set the Name to btnReset.
Step 3: Set the Text to Reset.
Step 4: Double-click on the button.
Step 5: Attach the code to reset the counter to 0 as shown below.
Example: Sales Tax Calculation
Create a simple application to calculate the sales tax for a
purchase. It will use a constant in order to indicate the sales tax
percentage.
Create Form
Step 1: Create a new project called Sales Tax Calculation.
Step 2: Set the Name property of the form to frmSalesTax.
Step 3: Set the Text property to Sales Tax Calculation.
Step 4: Right-click on Form1.vb in the Solution Explorer and
rename the form to frmSalesTax.vb.
Add Title Label
Step 1: Click on the label control in the Control toolbox.
Step 2: Draw a label control on the form.
Step 3: Set the Name property of the label to lblTitle.
Step 4: Change the Text property to Sales Tax Calculation.
Step 5: Change the Font Size property to 14 and the Font
Bold to True.
Step 6: Change the TextAlign property to MiddleCenter.
Add Purchase Price Label
Step 1: Place a label control on the form.
Step 2: Change the Name property to lblPurchasePrice.
Step 3: Change the Text property to Purchase Price.
Step 4: Change the Font Size property to 14 and the Font Bold property to True.