2. Logical
Operators
• Boolean Expressions
• Evaluates to True or False
• Can be formed using the logical
operator And and Or
• And
• Expression evaluates to True only
when both expressions (conditions)
are True
• If intGrade > 0 And intGrade < 100
Then
Me.lblDisplay.Text = “Valid
grade.”
4. Logical
Operators
• Or
• If either expression (condition) is
true, then the entire expression is
True.
• If intGuess < 1 Or IntGuess> 50
Then
Me.lblMessage.Text = “Invalid
number”.
6. Logical
Operators
• Not
• Is True only when the expression it
is used with is False
• Example:
• strItem = “Pen”
• If Not strItem = “Pen” Then
Me.lblMessage.Text = “No
discount”.
• Would evaluate to False since
strItem does equal “Pen”
8. Generating
Random
Numbers
• Random numbers are needed for
games, screen savers, and many other
types of applications.
• VB.NET has a built in Rnd() function
that generates random numbers.
• Using Rnd() alone generates number
greater than or equal to zero (0) and
less than one (1)
• What if a random number between 0
and 10 is needed?
9. Generating
Random
Numbers
• A range can be specified using this
formula
• (HighNumber – LowNumber + 1) *
Rnd() + LowNumber
• Example:
• We need a random number
between 10 and 30
• ((30-10+1 ) * Rnd() + 10)
• 21 * Rnd() + 10
• Using this formula we will have a
decimal number…Why?
10. Generating
Random
Numbers
• Rnd() generates number greater than or
equal to zero (0) and less than one (1)
• Rnd() always generates a decimal
• To generate an integer, the Int() function
can be used
• Int(21 * Rnd()) + 10
• The decimal potion of the calculation
is truncated
• Randomize()
• Initializes the Rnd() function
• Randomize() statement is added to the
event procedure before the Rnd()
function is used
11. Static Variables
• Variables have scope
• Local scope – declared in a
procedure
• Called a local variable
• Global scope – declared outside of
the procedure
• Called a global variable
• Variables also have a lifetime
• Local variable lifetime – duration of
the procedure
• Global variable lifetime – duration
of the program
12. Static Variables
• Lifetime of a local variable can be
extended by using a Static declaration
statement
• Static variableName as dataType = value
• Keyword Static replaces Dim
• Must be explicitly initialized in the
declaration
• Static variables
• Scope – local to the procedure
• Lifetime – duration of the program
• Necessary in event procedures with
variables that should be retained in
memory throughout program
execution
13. Counter
Variables
• A counter is a variable storing a
number that is incremented by a
constant value
• Useful for:
• Keeping track how many times a
button is clicked
• A guess is entered
• A password is typed
• A web site is visited
14. Counter
Variables
• Syntax:
• counter = counter + constant
• Counter is the numeric variable
that is updated
• Constant is the number that is
added to the current value of
the counter
• intNumTries = intNumTries + 1
• A counter should be declared as a
Static variable
15. The
Select…Case
Statement
• A decision structure that uses the
result of an expression to determine
which block of code to execute
• preferred to If…Then...ElseIf
• Takes the form:
Select expression
Case value
Statements
…
Case Else
Statements
End Select
16. The
Select…Case
Statement
Select Case intScore
Case 0, 10 'Score is 0 or 10
strMessage = "Nice try."
Case 20 To 25 'Score is 20, 21, 22, 23,
24, or 25
strMessage = "Great!"
Case Else 'Score other than 0, 10, 20,
21, 22, 23, 24, or 25
strMessage
End Select
‘Display message
Me.lblMessage.Text = strMessage
18. The
Select…Case Is
Statement
• Compares the result of an expression
to a range of values
• Syntax:
SELECT CASE variableName
CASE IS < value
execute statement 1
CASE IS = value
execute statement 2
CASE IS > value
execute statement 3
END SELECT
19. The
Select…Case Is
Statement
Select Case intTestGrade
Case Is > 100
strMessage = "Invalid grade."
Case Is >= 93
strMessage = "A"
Case Is >= 85
strMessage = "B"
Case Is >= 78
strMessage = "C"
Case Is >= 69
strMessage = "D"
Case Is >= 0
strMessage = "F"
Case Else
strMessage = "Invalid grade."
End Select
Me.lblMessage.Text = strMessage
20. Review
Displaying a
Message Box
• A message box is a predefined dialog
box that displays a message for the
user
• Uses:
• Alert the user to invalid data
• A reminder of options required for
an application to continue
• MessageBox is a class that uses the
Show() method for displaying
21. More About Displaying a
Message Box
• Syntax:
• MessageBox.Show(message)
• Message is a variable
• A constant
• Or a string enclosed in quotation
marks
• MessageBox.Show(“Guess out of
range”)
22. More About Displaying a
Message Box
• Syntax:
• MessageBox.Show(message, title, button,
icon)
• Message is displayed in the dialog box
• Title is in the title bar
• Additional buttons can be added
• An icon can be displayed
• MessageBox.Show(“Guess out of range”,
“Guessing Game”,
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error)
23. The CheckBox
Control
• Allows the user to select options
• One or more check boxes can be
selected
• CheckBoxes can be placed inside a
GroupBox object
• CheckBox object has the properties:
• Name
• Identifies the control object
• Has prefix chk
• Text
• Text displayed next to the box
• Checked
• Can be set to True or False to display the
check box with or without a check
24. The CheckBox
Control
• CheckBoxes can have click events
• If…Then statement can be used in a
program to determine if a check box is
selected or cleared.
• Example:
25. Line-
Continuation
Character
• Visual Basic.NET statements that are
long can be typed onto two or more
lines when the line-continuation
character is used.
• The underscore (_) is the line-
continuation character and MUST have
a space before it and nothing after it
and cannot occur within quotation
marks.
26. Line-
Continuation
Character
• Example:
If Not (Me.chkBed.Checked And
Me.chkLunch.Checked _
And Me.chkHomework.Checked _
And Me.chkTeeth.Checked) Then
MessageBox.Show("Did you forget
something?")
• Properly dividing a statement into two
or more lines can make code easier to
read, which is good programming style