0% found this document useful (0 votes)
10 views13 pages

VB Lecture II

The document discusses coding concepts like variables, data types, arithmetic expressions, and option statements. Variables are used to store and manipulate data in applications. The data type assigned to a variable determines what type of data it can hold and operations that can be performed on it. Methods like TryParse and ToString are used to convert between data types. Arithmetic expressions are used to perform calculations. Option statements help control data type conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

VB Lecture II

The document discusses coding concepts like variables, data types, arithmetic expressions, and option statements. Variables are used to store and manipulate data in applications. The data type assigned to a variable determines what type of data it can hold and operations that can be performed on it. Methods like TryParse and ToString are used to convert between data types. Arithmetic expressions are used to perform calculations. Option statements help control data type conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

Variables – Selecting an Appropriate Data Type



A variable’s data type indicates the type of data the variable will store;
 for example, numeric or textual.
 It also determines the variable’s size, which is the amount of memory it consumes.
Variables – Selecting an Appropriate Data Type


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 – Selecting an Appropriate Name



A variable’s name, also called its identifier, should describe (identify) its contents.

A good variable name should be meaningful right after you finish a program and also years
later when the program needs to be modified.
Variables – Examples

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

 numbers, letters, and so on – it is treated as a string in Visual Basic.


Even when the Text property contains only numbers, cannot be used as is
in a calculation;

 instead, it must be converted to its numeric equivalent.


One way of converting a string to a number is to use the TryParse method.

TryParse Method – Examples


TryParse Method

The three methods can convert a string that contains only numbers.


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:

 Option Explicit On above the Public Class clause in the Code


Editor window.


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

 9 is converted to 9.0 before it is assigned to dblLength



When a value is converted from one data type to another that can
store either larger numbers or numbers with greater precision, the
value is said to be promoted.
 Conversely, it is said to be demoted.
Option Statements

With implicit type conversions, data loss can occur when a value is
converted from one data type to a narrower data type,

 which is a data type with less precision or smaller capacity.


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

,
Area of Circle Code Example

You might also like