CSC104 Programming
Visual Basic Overhead Transparencies

Scope of Variables (8-19)

This example program doesn't do anything useful, but it does
demonstrate the difference between global variables, local variables,
and formal parameters.


(General)                 (Declarations)
Dim InBase As Integer                ' a global variable



Function Process (Value As Long) As String
    Dim Temp As String
    Temp = "Hello"

    Dim Index As Integer
    For Index = 1 To Len (Temp)

        Dim Count As Integer
        Count = Index + Value

        If Count > Index Then
            Dim Display As String
            Display = Temp & " There"
            Label2.Caption = Display
        End If

        InBase = Count
        Dim Temp2 As String
        Temp2 = ", "
        Temp = Temp & Temp2
    Next Index

    Label1.Caption = Temp
End Function

Eg. Even Numbers

This program reads in numbers from a user until the user enters the
number 99.  It adds up and prints all of the even numbers.  It also
prints the average value of the even numbers.


Dim SumEven As Integer
SumEven = 0

Dim Value As Integer
Value = InputBox ("Enter a number (99 to quit)", "Input window")

Do While (Value <> 99)
    If Value Mod 2 = 0 Then
        SumEven = SumEven + Value
        CountEven = CountEven + 1
    End If
Loop

If CountEven <> 0 Then
    Label1.Caption = "Sum of Even Numbers is " & SumEven & vbNewLine
    Label1.Caption = Label1.Caption & "Even Number Average: " & SumEven/CountEven
End If

Sample Input:
   2
   3
   6
   7
   4
   99

Here is what happens as a the loop executes:

ValueSumEvenCountEven
221
321
682
782
4123
99123
Label1.Caption"Sum of Even Numbers is 12
Average of Even Numbers is 4"

Eg. Palindrome

Private Sub Command_Click()
    ' This program determines if the word entered is a
    ' palindrome.  This is a word, if spelt backwards gives
    ' the same word as spelled forwards.
    
    Dim Word, Char, NewWord As String
    Dim Length As Integer
    
    Word = Text1.Text   ' Get word from the text box
    Length = Len(Word)  ' Get length of string
    
    NewWord = ""        ' Initialize new reversed word
    
    ' Reverse word (or could have used a for loop)
    Do While (Length > 0)
        Char = Mid(Word, Length, 1)
        NewWord = NewWord & Char
        Length = Length - 1
    Loop
    
    ' See if word and the reversed word are the same
    If Word = NewWord Then
        Text1.Text = "Yes"
    Else
        Text1.Text = "No"
    End If
End Sub

Assignment 2 - Using Functions

I'll discuss this part of Assignment 2 during the lecture.


Function Bin2Dec (ByVal Number As String) As String
    .
    .
    .
    Bin2Dec = ____________  ' return result here
End Function


Sub ConversionDriver ()
    .
    .
    .
        OutputValue =  Bin2Dec (InputValue)   ' function call
    .
    .
    .
End Sub