100% found this document useful (1 vote)
167 views

Variables, Constants and Calculations

This document discusses variables, constants, and calculations in visual programming. It defines variables as storing data values not in files and being assigned a unique name. Variables have different data types like integer and short to store different value types and ranges. Constants are similar to variables but cannot change value. Arithmetic operators like addition and subtraction are covered along with order of precedence. An example counter application and sales tax calculation application are provided to demonstrate using variables, constants, and calculations.

Uploaded by

Millen Marbun
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
167 views

Variables, Constants and Calculations

This document discusses variables, constants, and calculations in visual programming. It defines variables as storing data values not in files and being assigned a unique name. Variables have different data types like integer and short to store different value types and ranges. Constants are similar to variables but cannot change value. Arithmetic operators like addition and subtraction are covered along with order of precedence. An example counter application and sales tax calculation application are provided to demonstrate using variables, constants, and calculations.

Uploaded by

Millen Marbun
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Variables, Constants and

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

Short Small integer numbers -32,768 to 32,767


Single Small floating point numbers Floating point number with up to 6 digits of
accuracy
String Character data Varies based on the number of characters
Variable Names
A variable name in Visual Basic .NET begins with a letter and may be
followed by any combination of letters, underscores, or digits. It can be as
small as one letter or as large as 255 letters, underscores, and digits
combined.
Variable name should be representative of the value that it is storing. More
readable variable names will make the program easier to follow.
Visual Basic .NET does not differentiate between two variable names that
are identical except for the case of the letters in their names. Therefore, it
is not case sensitive with regard to variable names.
Letters used in variable names can be either lowercase or uppercase. If
you refer to the variable with a different capitalization later in the program,
Visual Basic .NET will convert the capitalization to the one used earlier in
the program.
Variable Names
Visual Basic .NET will immediately provide feedback if you have
violated the rules of declaring a variable: an underline will
appear under the violating text.
If you then move the mouse pointer over the underlined text, a
pop-up message with an explanation of the error will appear.
Declaring Variables
• Declare local variables with the Dim statement and module-level
variables with the Private statement. 
• Example:
Dim StudentNameString As String
Dim StudentNameString, MajorString As String
Private TotalDecimal As Decimal
Declaring Constants
• Declare constants with the Const statement.
• Constants are similar to variables – constants are values that are
stored to memory locations; however, a constant cannot have its
value change during program execution – constant values are
generally fixed over time.
• Example: Sales Tax Rate
Const COURSE_TITLE_STRING As String = ""Programming Visual Basic""
Arithmetic Operators
The arithmetic operators are the same as in many other programming
languages. They are:
+   Addition
-   Subtraction
*   Multiplication
/    Division
^   Exponentiation
\    Integer Division
Mod  Modulus Division
Order of Precedence

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.

Add Sales Tax Label


Step 1: Place a label control on the form.
Step 2: Change the Name property to lblSalesTax.
Step 3: Change the Text property to Sales Tax.
Step 4: Change the Font Size property to 14 and the Font Bold property to True.
Add Final Price Label
Step 1: Place a label control on the form.
Step 2: Change the Name property to lblFinalPrice.
Step 3: Change the Text property to Final Price.
Step 4: Change the Font Size property to 14 and the Font
Bold property to True.
Add Purchase Price Text Box
Step 1: Place a text box control on the form.
Step 2: Change the Name property to txtPurchasePrice.
Step 3: Erase the value in the Text property.

Add Sales Tax Text Box


Step 1: Place a text box control on the form.
Step 2: Change the Name property to txtSalesTax.
Step 3: Erase the value in the Text property.
Add Final Price Text Box
Step 1: Place a text box control on the form.
Step 2: Change the Name property to txtFinalPrice.
Step 3: Erase the value in Text property.
Add Calculation Button
Step 1: Place a button control on the form.
Step 2: Set the Name property of the control to btnCalculate.
Step 3: Change the Text property to Calculate.
Add Code to the Button
Step 1: Double-click on the btnCalculate button.
Step 2: Type the declaration to define a constant called decSalesTaxRate as a Decimal data type
and set it equal to 0.05.
Step 3: Declare three variables: decSalesTaxAmount, decFinalPrice, and
decPurchasePrice.
Step 4: Convert the value stored in the txtPurchasePrice text box to a numerical value and store
it in the decPurchasePrice variable.
Step 5: Calculate the decSalesTaxAmount by multiplying the decSalesTaxRate by
decPurchasePrice.
Step 6: Calculate the decFinalPrice by adding the amount stored in the decPurchasePrice and
decSalesTaxAmount.
Step 7: Store the decSalesTaxAmount in the txtSalesTax text box.
Step 8: Store the decFinalPrice in the txtFinalPrice text box.
Case Study
This case study will add the computational functionality that was
missing. You need to add a btnCalculate button that will
automatically calculate a person’s weekly pay as well as the total
payroll cost.
A person’s weekly pay is calculated by multiplying the number of
hours a person worked by a fixed hourly rate of $9.50/hour.
Final Result

You might also like