VB Lecture II
VB Lecture II
Coding with Variables, Named Constants, and Calculations
Variables, TryParse Method, Arithmetic Expressions, ToString Method, Option Statements, &
Named Constants
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.
E.g. include all of the user-provided items (from your Planning Chart) that will be
included in a calculation and also result of any calculation made by the application.
Storing data in variables allow 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.
It also makes your code run more efficiently because the computer can process data stored
in a variable much faster than it can process data stored in the property of a control.
Variables – Declaration
The Dim statement is often used to declare variables within
procedures..
The statement assigns a name, a data type, and an initial
value to the variable.
E.g.
Dim intQuantity As Integer
declares a variable whose name is intQuantity , data type is Integer,
and initial value is the number 0.
Decimal or Double data type store numbers containing decimal
places.
The two data types 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.
However, calculations involving Decimal variables are not subject to
the small rounding errors that may occur when using Double variables;
this is due to the Decimal data type storing numbers with many more
significant digits.
Variables – Examples
Variable declaration within a btnCalc_Click procedure
TryParse Method
When the user enters data in a text box during run time, the computer
stores the data in the control’s Text property.
Because the Text property can contain any characters
Even when the Text property contains only numbers, cannot be used as is
in a calculation;
One way of converting a string to a number is to use the TryParse method.
They can also convert a string that contains a leading sign as well as one that
contains leading or trailing spaces.
In addition, the Double.TryParse and Decimal TryParse methods can convert a
string that contains a decimal point or a comma.
However, none of the three methods can convert a string that contains;
a dollar sign, a percent sign, a letter, a space within the string, or an empty string.
TryParse Method
Arithmetic Expressions
Arithmetic Expressions
The integer division operator (\) divides two integers and then returns the result as
an integer.
Using this operator, the expression 9 \ 4 results in 2, which is the integer result of
dividing 9 by 4.
The arithmetic expressions you enter in your code should not contain commas or
special characters;
E.g. to multiply 30 by 5%, use the expression 30 * 0.05 instead of 30 * 5%.
ToString Method
The need will always arise for one to display a computed numeric value to object on a
GUI (form).
Note, these objects only process data as Text and therefore, there is the need to
convert it to string before assigning to the Text property.
The ToString method is used to make the conversion.
All of the numeric data types have a ToString method for this purpose.
The ToString method also allows you to format the value, which means to specify
the number of decimal places and the special characters (such as a dollar sign,
percent sign, or thousands separator) to display.
The ToString method formats a copy of the value stored in the numeric variable and
then returns the result as a string.
ToString Method
ToString Method
ToString Method
To compute area of a circle and display on a label.
lblArea.Text = dblArea.ToString("N2")
Option Statements
A variable’s declaration statement allows you to control the variable’s
data type.
To ensure that all of the variables in your code have been declared
insert the statement:
The Option Infer Off statement, tells the Code Editor to ensure that
every variable is declared with a data type.
This is because the statement tells the computer not to infer (or assume) a memory
location’s data type based on the data assigned to it.
Option Statements
implicit type conversion; convert a value to fit a memory location, if
the value’s data type does not match the memory location’s data
type.
E.g., when processing the statement Dim dblLength As Double = 9
You can eliminate the problems that occur as a result of implicit type
conversions by entering the Option Strict On statement above the
Public Class clause in the Code Editor window.
When Option Strict On is set the the following rules are used for
conversions.
Option Statements
Area of Circle Code Example
To compute area of a circle and display on a label.
,
Area of Circle Code Example