Concepts of Computing, Section II

Visual Basic Language

Back to course page.


Layout

Comments extend from the quote mark ' to the end of the line.

Case (upper or lower) is not significant. As you type the code, the VB editor automatically sets keywords in bicapitalized form, changing your end if to End If etc.

Line breaks are significant. It must be

 If condition Then statement

or

 If condition Then
   statements
 End If

but not

 If condition
   Then statement

You can break a single statement across lines by putting an underscore character _ at the end of the line, like this:

 If very long condition _
 Then
   statements
 End If

Declarations and variables

      Dim variable As Type

Example:

      Dim phase As Integer

Controls and properties

      ControlName.Property

Example:

      lblTotal.Caption

Assignment, arithmetic expressions

      variable or property = expression

Example:

      lblTotal.Caption = lblTotal.Caption + 1

Function call

      FunctionName(expression)

Example:

      Int(2 * Rnd()) 

If ... Then ... Else

    If condition Then
        statements
    Else
        statements
    End If

Example:

    If Int(2 * Rnd()) Then
        lblHeads.Caption = lblHeads.Caption + 1
    Else
        lblTails.Caption = lblTails.Caption + 1
    End If

Select Case

    Select Case expression
        Case expression
	    statements
        Case expression
	    statements
          .
          .
          .
    End Select

Example:

    Select Case phase
        Case green
            phase = yellow
            shpGreen.FillStyle = transparent
            shpYellow.FillStyle = solid
        Case yellow
            phase = red
            shpYellow.FillStyle = transparent
            shpRed.FillStyle = solid
        Case red
            phase = green
            shpRed.FillStyle = transparent
            shpGreen.FillStyle = solid
    End Select

Form load subroutine

  Private Sub Form_Load()
      statements
  End Sub

Example:

Private Sub Form_Load()
    '
    ' Define the constants - color coding is arbitrary
    '
    green = 0
    '
    ' Starting phase
    '
    phase = green
End Sub

Event subroutine

  Private Sub ControlName_Event()
      statements
  End Sub

Examples:

  Private Sub cmdFlip_Click()
      lblTotal.Caption = lblTotal.Caption + 1
      '
      ' Rnd returns a random number between 0.0 and 1.0
      ' Int returns the integer portion of a number
      '
      If Int(2 * Rnd()) Then
          lblHeads.Caption = lblHeads.Caption + 1
      Else
          lblTails.Caption = lblTails.Caption + 1
      End If
  End Sub
  Private Sub cmdExit_Click()
      End
  End Sub

Other web pages

Back to top


Jon Jacky, jackyj@evergreen.edu