Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Programming with Microsoft Visual Basic 2017

Chapter 3
Coding with
Variables, Named
Constants,
and Calculations

• Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
FOCUS ON THE CONCEPTS LESSON
(1 of 2)
Concepts covered in this lesson:
• F-1 Pseudocode /planning steps (it is important!)
• F-2 Main memory of a computer
• F-3 Variables
• F-5 Arithmetic expressions
• F-6 Assigning a value to an existing variable
• F-7 ToString method
• F-8 Option statements
• F-9 Named constants
• Procedure-level variables
• How to convert the planning steps to Visual Basic
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-1 Pseudocode (1 of 3)
Many programmers use planning tools such as pseudocode.
• Pseudocode/Planning steps uses short phrases to
describe the steps a procedure must take to accomplish its
goal. e.g.
– Declare input and output variables
– Accept 2 numbers
– Calculate the sum
– Display the sum

• Programmers use the planning tool of their choice as a


guide when coding the procedure. We will demonstrate
why the planning/pseudocode steps is so important in
a little while.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-2 Main Memory of a Computer (1 of 3)
• The main memory of a computer is called random
access memory or, more simply, RAM.
• The items stored in RAM include the operating system
(Windows), application programs (such as Visual
Studio), and data (such as the amount entered in the
txtAmount control or the sum calculated by the
application).
• Each memory location in RAM has a unique numeric
address.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-2 Main Memory of a Computer (2 of 3)
• Reserving a memory location is more commonly
referred to as declaring a memory location.
• There are two types of memory locations that a
programmer can declare:
– variables and
– named constants.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-2 Main Memory of a Computer (3 of 3)

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-3 Variables

• A variable is a computer memory location where a


programmer can temporarily store an item of data
while an application is running.
• The memory location is called a variable because its
contents can change (vary) during run time.
• Storing the data in a variable allows the programmer to
control the preciseness of the data, verify that the data
meets certain requirements, and save the data for later
use within the application’s code.
• Dim statement declare a variable within the procedure
that needs it.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type

A variable’s data type indicates the type of data—for


example, numeric or textual—the variable will store.
• Variables assigned the Integer data type can store
integers (whole numbers) , which are positive or
negative.
• The two data types Decimal or Double data type:
– differ in the range of numbers each can store and the
amount of memory each needs to store the numbers.
– Calculations involving Double variables execute faster
than those involving Decimal variables.
– Decimal is used for monetary values

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type
• The String data type can store from zero to
approximately 2 billion characters.
• The Boolean data type stores Boolean (or logical)
values: either True or False.
• The Char data type store one Unicode character.
• Examples:
• Integer: 15 or 500024 or -88 or 4577711111
• Double: 15.00001 or -55.0 or 40000000.45
• Decimal: 1.000004 or -104.33 or 80000000.0
• String: “VUT” or “Vanderbijlpark” or “Sebokeng”
• Char: “7” or “r” or “R” or “(“ or “<“
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Name

• A variable’s name, also called its identifier, should


describe (identify) its contents.
• A good variable name is meaningful right after you
finish a program and also years later when the program
needs to be modified.
• The name allows the programmer to refer to the
variable using a descriptive word, rather than its
cryptic numeric address, in code. Descriptive words are
easier to remember and serve to self-document your
code.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Name

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-5 Arithmetic Expressions
Most commonly used operators:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-5 Arithmetic Expressions

Most applications require the computer to perform at least


one calculation. You instruct the computer to perform a
calculation using an arithmetic expression, which is an
expression that contains one or more arithmetic operators.
• The integer division operator (\) divides two integers
and then returns the result as an integer.
• The modulus operator (sometimes referred to as the
remainder operator) also divides two numbers, but the
numbers do not have to be integers. After dividing the
numbers, the modulus operator returns the remainder
of the division.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-6 Assigning a Value to an Existing
Variable
An assignment statement is also used to assign a
value to a variable during run time.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-6 Assigning a Value to an Existing
Variable
To calculate the circle’s area and then assign it to a
variable:

• area = 3.14159 * radius ^ 2

• or

• area = 3.14159 * (radius * radius)

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-7 To String Method

• The ToString method also allows to format the value,


which means to specify the number of decimal places
and the special characters to display.

• The ToString method formats a copy of the value


stored in the numeric variable and then returns the
result as a string.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-7 To String Method

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

To enter the Option statements :


• Add 2 blank lines immediately above the Public Class
clause and. Enter the following two Option statements:

Option Explicit On
Option Strict On

Pubic Class FrmMain

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

• Option Strict On
– Stop automatic (implicit) conversions
– (This will stop you from converting between the
wrong types by accident)

• Option Explicit On
– All variables must be declared
– (you cannot just use a variable without a Dim
statement for it)

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

• Implicit (Automatic) conversion:


– When a value is converted from one data type to
another data type that can store either larger numbers or
numbers with greater precision

• Explicit (Programmer Forced) conversion:


– When a value is converted from one data type to
another data type that can store only smaller numbers or
numbers with less precision

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
and variable assignments
• The type of the variable determine the data that can be
saved in it:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

• We need to convert a Double if we want to save that


value in a Decimal:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

• We cannot store a Double in a Integer without


conversion:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements

• Just adding ANY conversion is also not correct:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants

• Like a variable, a named constant is a memory


location inside the computer. However, unlike the value
stored in a variable, the value stored in a named
constant cannot be changed while the application is
running.
• To differentiate the name of a constant from the name
of a variable, programmers uppercase all of the
characters in a constant’s name.
• Declare a named constant using the Const statement.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants
Syntax for Const statement

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants (3 of 4)

• A constant name must tell me which data is saved.


• Good constant names are:
– Const VAT As Double = 0.15
– Const PI as Double = 3.14159
– Const MAXIMUM_VALUE As Integer = 20

• BAD constant names gives the value:


– Const FIFTEEN As Double = 0.15
– Const THREE_POINT_SOMETHING as Double = 3.14
– Const TWENTY As Integer = 20

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-1 Determine a Memory Location’s Scope
and Lifetime
• The scope indicates where the declared memory
location can be used in an application’s code,
• and the lifetime indicates how long the variable or
named constant remains in the computer’s main
memory.
• The scope and lifetime are determined by where you
declare the memory location in your code.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-1 Determine a Memory Location’s Scope
and Lifetime
• Memory locations declared in a procedure have
procedure scope and are called procedure-level
variables and procedure-level named constants.
• (Thus they can only be used in that procedure)

• Memory locations declared in a form class’ declarations


section, but outside of any procedures, have class
scope and are referred to as class-level variables
and class-level named constants. Not used
currently. You will LATER learn WHEN to use this.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-2 Use Procedure-Level Variables

Procedure-level variables are declared at the beginning


of a procedure, and they can be used only after their
declaration statement within the procedure. The variables
remain in the computer’s main memory only while the
procedure is running, and they are removed from memory
when the procedure ends.
• The TaxApplication (a few slides back) illustrates the
use of procedure-level variables.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-3 Use Procedure-Level Named
Constants
• Like procedure-level variables, procedure-level named
constants are declared at the beginning of a procedure,
and they can be used only after their declaration
statement within the procedure.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning.
• At the end of the Study/Leaner guide in Appendix F you
will find a list if initial VB.NET statements and their
meanings. This will assist students!!

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning.
• Open the Planning of the Tax Application (Week 3).
• You know already how to create the solution and how
to design the screen using the planning.

• We will now convert each of the Pseudocode/Planning


Steps to VB statements:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
1. The first step is to add the comments and the option
statements. Press F7 to open the Code and fill it in:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
2. Now we will take the button planning/pseudocode steps
and follow each one. So we must declare the variables:

We will use Dim statements to declare all the variables


specified in the planning. We will also declare all constant
values:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
3. The next step is to get the amount.
We need to get the amount that the
user entered in the text box txtAmount
And store it in the variable:.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
When we type the statement is gives a conversion error.
Click on the red block below txtAmount.Text to open the
error message and click on the Replace message.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
The Replace adds the conversion (From String to
Decimal) for us. (The Text property stores a String value):

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
4. The next step is to:
calculate the tax amount
We add the formula:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
5. The last step is to:
display the tax amount
We add the statement:

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
Again there is a conversion error that we are warned
about. When we display output we want to format the
output (e.g. display a rounded value). So now we will add
a call to the toString function. For monetary values we
will always specify that we want currency formatting:
(So for output we will always call the toString function)

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary

• Programmers use planning tools, such as flowcharts


and pseudocode, when planning the steps a procedure
needs to take to accomplish its goal.

• The main memory of a computer is called random


access memory (RAM).
• Variables are memory locations used by programmers
to store data during run time. The value stored in a
variable can change while the application is running.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• User-provided items (input) included in a calculation
should be stored in variables.
• The results of calculations made by the application
should also be stored in variables.
• In most procedures, the Dim statement is used to
declare a variable.
• Each data type is a class from which a variable can be
instantiated.
• The value stored in a control’s Text property is always
treated as a string.
• You can use the CInt, CDec or CDbl calls to convert a
string to a number.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• The arithmetic operators have an order of precedence.
You can use parentheses to override the normal order
of precedence.
• The integer division (\) operator divides two integers
and then returns the result as an integer.
• The modulus (Mod) operator divides two numbers and
then returns the remainder of the division.
• When an expression contains more than one operator
with the same precedence number, those operators are
evaluated from left to right.
• Arithmetic expressions should not contain dollar signs,
commas, or percent signs.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary

• The data type of the expression assigned to a variable


should match the variable’s data type.
• You can use the literal type character D to convert a
Double number to the Decimal data type.
• You can use the ToString method to convert a numeric
value to a string. The method also allows you to specify
the number of decimal places and the special
characters to include in the string.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• To ensure that all of your variables have been declared,
enter the Option Explicit On statement above the Public
Class clause in the Code Editor window.

• To prevent the computer from making implicit type


conversions that may result in a loss of data, enter the
Option Strict On statement above the Public Class
clause in the Code Editor window.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary

• You use the Const statement to declare a named constant.


• A memory location’s scope indicates where the
variable or named constant can be used in an
application’s code.
• A memory location’s lifetime indicates how long the
variable or named constant remains in the computer’s
main memory.
• Variables and named constants declared in a
procedure have procedure scope and can be used only
after their declaration statement in the procedure.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary

• A procedure does not have access to the memory


locations declared in a different procedure.
• An application’s variables are declared using the
minimum scope needed, which usually is
procedure scope.

Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part

You might also like