CSC301 - Chapter 2

You might also like

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

CHAPTER 2:

VARIABLE, CONSTANTS & CALCULATIONS


The concept of Event-Driving Programming

Variables

TryParse method

Arithmetic expressions

Assigning a value to an existing variable

ToString method

Named Constants
THE CONCEPT OF
EVENT-DRIVEN
PROGRAMMING
WHAT IS EVENT-DRIVEN?
 Code is executed in response to events trigger by the user actions.
 E.g.: Clicking the mouse, pressing a key on the keyboard, selecting an item
from a drop-down list, typing some words into textbox, etc.
 Common events in VB 2017 are load, click, double-click, drag and
drop, pressing the keys and more.
 Every form and every control placed on the form has a set of events
related to them.
 To view the events, double-click the control (object) on the form to enter. the
code window.
THE LOAD The code appears on the left Click on the default event
side is the event procedure to view other events
EVENT associated with the load event. associated with the control

 Figure 2.1 illustrates


the event procedure
load associated with
the Form (its name
has been changed to
PicViewer therefore
you can see the
words PicViewer
events)

Figure 2.1: Events associated with Form


THE CLICK
EVENT
 Figure 2.2 shows
the events
associated with
button.

Figure 2.2: Events associated with the button


WRITING THE CODE

 To start writing the code, click on any part of the form to go into the code window
as shown in Figure 2.1.
 The event procedure is to load Form1 and it starts with the keywords Private Sub
and ends with End Sub.
 This procedure includes the Form1 class and the event Load, and they are bind
together with an underscore, i.e. Form_Load.
 It does nothing other than loading an empty form.
VARIABLES
WHAT IS VARIABLES?

 A computer memory location where a programmer can temporarily


store an item of data while an application is running.
 Its contents can change (vary) during run time.
WHAT CAN YOU DO WITH VARIABLES?

 Copy and store values entered by the user, so they may be manipulated
 Perform arithmetic on values
 Verify values to determine that they meet some requirements
 Temporarily hold and manipulate the value of a control property
 Remember information for later use in the program
DECLARING VARIABLES

 A variable declaration is a statement that creates a variable in memory


 The syntax is:
Dim VariableName As DataType
 Dim (short for Dimension) is a keyword
 VariableName is the programmer designated name
 As is a keyword
 DataType is one of many possible keywords for the type of value the variable will contain
 Here is an example of a variable declaration:
Dim intLength as Integer
SELECTING AN
APPROPRIATE DATA
TYPE (1 OF 2)

 A variable’s data type


indicates the type of
data—for example,
numeric or textual—the
variable will store.
 It also determines the
variable’s size, which is
the amount of memory
it consumes.
SELECTING AN APPROPRIATE DATA TYPE (2 OF 2)
 The Boolean data type
 stores Boolean (or logical) values: either True or False.
 The Decimal data type
 Decimal is the recommended data type for application that performs complex calculations involving
money, where you need accuracy to the penny.
 The Double data type
 Calculations involving Double variables execute faster than those involving Decimal variables.
 The Integer data type
 can store integers, which are positive or negative numbers that do not have any decimal places.
 The String data type
 can store from zero to approximately 2 billion characters.
SELECTING
AN
APPROPRIATE
NAME
A VARIABLE’S NAME, ALSO
CALLED ITS IDENTIFIER,
SHOULD DESCRIBE (IDENTIFY)
ITS CONTENTS.
EXAMPLES OF VARIABLE
DECLARATION
STATEMENTS

 Figure shows the Dim


statement’s syntax and
includes several examples of
using the statement.
TRYPARSE METHOD (1 OF 3)

 Data entered by the user in a text box (numbers, letters and so on) is treated as a
string in Visual Basic.
 Even when the Text property contains only numbers, it is still considered a string and
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.
 Every numeric data type in Visual Basic has a TryParse method that converts a string
to that particular data type.
TRYPARSE
METHOD (2
OF 3)
TRYPARSE METHOD
(3 OF 3)

 TryParse method parses


its string argument to
determine whether the
string can be converted
to a number.
 The term “parse” means
to look at each
character in the string.
ARITHMETIC
EXPRESSIONS
COMMONLY
USED
ARITHMETIC
OPERATORS (1
OF 2)
COMMONLY USED ARITHMETIC OPERATORS (2 OF 2)

 Integer division operator (\)


 Divides two integers and then returns the result as an integer.
 For example, 9 \ 4 results in 2, which is the integer result of dividing 9 by 4.
 If the standard division operator [/] is used to divide 9 by 4, the result is 2.25
rather than 2.
 Modulus operator (Mod)
 Divides two numbers, but the numbers do not have to be integers.
 For example, the expression 9 Mod 4 equals 1, which is the remainder of 9 divided
by 4.
ASSIGNING A VALUE TO
AN EXISTING VARIABLE

 An assignment statement is used to assign a


value to a variable during run time.
 When the computer processes an assignment
statement, it evaluates the expression first and
then stores the result in the variable.
 A variable can store only one value at any one
time.
 When another value is assigned to a variable,
the new value replaces the existing one.
TOSTRING METHOD (1 OF 2)

 The ToString method to convert a numeric value to a string.


 For example, to assigns the number stored in the dblArea variable to the
lblArea.Text property; you must convert the number to a string because the
Text property stores only strings.
 The ToString method is used to make the conversion.
 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.
TOSTRING
METHOD (2
OF 2)
NAMED CONSTANTS
WHAT IS NAMED CONSTANTS?

 Named constant is a memory location


inside the computer.
 The value stored in a named constant
cannot be changed while the application
is running.
 Advantage: If the value changes in the
future, you will need to modify only the
named constant’s declaration statement,
rather than all of the program statements
that use the value.
 A named constant using the Const
statement.
END OF CHAPTER 2

You might also like