3. Variables and Constants
Variable: Memory locations that hold data that can be
changed during project execution
Example: customer’s name
Named Constant: Memory locations that hold data
that cannot be changed during project execution
Example: sales tax rate
4. Using Variables and Constants
In Visual Basic when you declare a Variable or Named
Constant
An area of memory is reserved
A name is assigned called an Identifier
6. Naming Variables & Constants
Must follow Visual Basic Naming Rules
Cannot use reserved words or keywords that Basic has
assigned a meaning such as print, name, and value
Must begin with a letter and no spaces or periods
Should follow Naming Conventions
Names should be meaningful
Include class (data type) of variable in name
Use mixed case for variables and uppercase for
constants
7. Scope and Lifetime of Variables
Visibility of a variable is its scope
Where is that identifier valid?
Scope may be
Namespace: throughout project
Module: within current form/class
Local: within a procedure
Block: within a portion of a procedure
Lifetime of a variable is the period of time the variable
exists
8. Static Variables
Use static to declare local and block level variables
that need to retain their value
Variable will not be initialized next time procedure
runs, and will have last value assigned
If the variable is used in multiple procedures, declare
it at the module level with private
Pass data as arguments if one procedure calls the other
9. Converting Strings to Numeric Values
Use Parse methods to convert a string to its numeric
value before it’s used in a calculation
Each numeric data type class has a Parse method
Parse method returns a value that can be used in
calculations
Parse method fails if user enters nonnumeric data,
leaves data blank, or entry exceeds data type size
10. Try/Catch Blocks
Enclose statements that might cause a run-time error
within Try/Catch block
If an exception occurs while statements in the Try
block are executing, code execution is moved to the
Catch Block
If a Finally statement is included, the code in that
section executes last, whether or not an exception
occurred
11. Using Overloaded Methods
This OOP feature allows the Messagebox Show
method to act differently for different arguments
Each argument list is called a signature: the Show
method has multiple signatures
Supplied arguments must exactly match one of the
signatures provided by the method
12. If Statements
Used to make decisions
If true, only the Then clause is executed; if false, only
Else clause is executed (if there is an Else)
Block If…Then…Else must always conclude with End
If
Then must be on same line as If or ElseIf
End If and Else appear alone on a line
13. Conditions
Test in an If statement is typically based on a
condition
Six relational operators are used for comparison of
numbers, dates, and text
Equal sign is used to test for equality
Strings are compared using ANSI value of each
character
CIS is less than CNA
MATH is less than MATH&
14. Combining Logical Operators
Compound conditions can combine multiple logical
conditions
And describes conditions where both tests are true
Or describes conditions where either or both tests are
true
When both And and Or are evaluated And is
evaluated before the Or
Use parenthesis to change the order of evaluation
15. Input Validation
Check to see if valid values were entered or available
Can check for a range of values (often called
“reasonableness”)
If Integer.Parse(scoreTextBox.Text) >= 0 Then
‘ Code to perform calculations….
Check for a required field (not blank)
If studentIDTextBox.Text <> "" Then ...
16. Select Case
Use Select Case to test one value for different
matches (“cases”)
Usually simpler and clearer than nested If
No limit to number of statements that follow a Case
statement
When using a relational operator must use the word
Is
Use the word “To” to indicate a range with two
endpoints
17. Sharing an Event Procedure
Add object/event combinations to the Handles clause
at the top of an event procedure
Allows the procedure to be associated with different
events or other controls
Sender argument identifies which object had the
event happen
Cast (convert) sender to a specific object type using
the CType function
18. Calling Event Procedures
Calling an event procedure allows reuse of code
[Call] ProcedureName (arguments)
Keyword Call is optional and rarely used
Examples
Call clearButton_Click (sender, e)
--OR--
clearButton_Click (sender, e)
19. Breakpoints
Breakpoints allow you to follow the execution of your
code while program is running
Can hover the cursor over a variable or property to see
the current value in the current procedure
Can execute each line, skip procedures
Can use Console.Writeline to output values to track
code execution
Variables and property values can be seen in different
windows (autos, locals) while code is executing
20. Writing Procedures
A general procedure is reusable code which can be
called from multiple procedures
Useful for breaking down large sections of code into
smaller units
Two types:
Sub Procedure performs actions
Function Procedure performs actions AND returns a
value (the return value)
21. Passing Arguments to Procedures
If a procedure includes an argument, any call to the
procedure must supply a value for the argument
Number of arguments, sequence and data type must match
Arguments are passed one of two ways:
ByVal – Sends a copy of the argument’s value, original cannot
be altered
ByRef - Sends a reference to the memory location where the
original is stored and the procedure may change the
argument’s original value
If not specified, arguments are passed by value
22. Modal versus Modeless Forms
Show method displays a form as modeless - means
that both forms are open and the user can move from
one form to the other
ShowDialog method displays a new form as modal -
the user must close the form in order to return to the
original form
No other program code in the original form can execute
until the user responds to and hides or closes the modal
form
23. ListBoxes and ComboBoxes
Provide the user with a list of choices to select from
Various styles of display, choose based on
Space available
Need to select from an existing list
Need to add to a list
Listboxes and comboboxes share most of the same
properties and operate in a similar way
Combo box control has a DropDown Style property
Combo box allows text entry
24. The Items Collection
List of items in a ListBox or ComboBox is a collection
Collection is group of like objects
Items referenced by number (zero-based)
Collections are objects that have properties and
methods that allow
Adding items
Removing items
Referring to an individual element/member
Counting items
25. SelectedIndex
Index number of currently selected item is stored in
the SelectedIndex property
Property is zero-based
If no list item is selected, SelectedIndex property is
negative 1 (-1)
Use to select an item in list or deselect all items
26. Do Loops
A loop repeats a series of instructions
An iteration is a single execution of the statement(s) in the
loop
Used when the exact number of iterations is unknown
A Do/Loop terminates based on condition change
Execution of the loop continues while a condition is True or
until a condition is True
The condition can be placed at the top or the bottom of
the loop
27. Pretest vs. Posttest
Pretest: test before enter loop
loop may never be executed since test executes
BEFORE entering loop
Do While … Loop
Do Until … Loop
Posttest: test at end of loop
loop will always be executed at least once
Do … Loop While
Do … Loop Until
28. For/Next Loops
Used to repeat statements in a loop a specific number of
times
Uses a numeric counter variable called Counter or Loop
Index
Counter is incremented at the bottom of the loop on each
iteration
Start value sets initial value for counter
End value sets final value for counter
Step value can be included to specify the incrementing
amount
Step can be a negative number
29. For/Each Loops
Used to repeat statements for each member of a
group
A reference variable is used to “point” to each item
Must be the data type of each item in group
30. Exiting Loops
In some situations you may need to exit the loop early
Use the Exit For or Exit Do statement inside the loop
Typically used in an If statement (some condition
determines whether to exit the loop or not)
31. Arrays
List or series of variables all referenced by the same
name
Similar to list of values for list boxes and combo boxes,
without the box
Each variable is distinguished by an index
Each variable is the same data type
Individual elements are treated the same as any other
individual variable
32. Array Terms
Element: Individual item in the array
Subscript (or index): Zero-based identifier used to
reference the specific elements in the array
Must be an integer
Subscript Boundaries
Lower Subscript, 0 by default
Upper Subscript
33. Subscripts
Subscripts may be constants, variables, or numeric
expressions
Subscripts must be integers
Subscripts begin at zero (0)
VB throws exceptions for subscripts that are out of
range (upper and lower bounds).