Concepts of Computing, Section II

Visual Basic Language II

Back to course page.


Data types

    Integer
    Single
    Double
    String

Use in declarations

    Dim variable As Type

Example:

    Dim x As Double

Array declaration

    Dim var(size) As Type

Example:

    Dim list(10) As Integer ' a list of 10 numbers

Array reference

    var(index)

Example:

    list(i)

Function definition

ByRef is the default parameter passing mechanism.

    Private Function Name(ByVal var As Type, ByRef var As Type, ...) As Type
        ...
        statements
	...
        Name = var
    End Function

Example:

    Private Function Root(ByVal a As Single, ByVal t As Single, _
                        ByRef i As Integer) As Single
        ...			
        statements
	...	
        Root = x
    End Function

Function call

    var = Name(var, var, ...)

Example:

    r = Root(x, e, n)

Do While loop

    Do While condition
        ...
	statements
	...
    Loop

Example:

    Do While Not EOF(1) And nlines < 20 ' don't overrun array
        nlines = nlines + 1
        Line Input #1, lines(nlines)
	...
        Debug.Print nlines; lines(nlines)
    Loop

Do Until loop

    Do
        ...
	statements
	...
    Loop Until condition

Example:

    Do
        x = (x + a / x) / 2# ' improve the approximation
        d = x * x - a ' calculate difference
        i = i + 1 ' count this iteration
        Debug.Print "i "; i; "  guess "; x; "  diff "; d
    Loop Until Abs(d) < t

also Do Until ... Loop and Do ... Loop While ...


For loop

    For index = start To end
        ...
	statements
	...
    Next index

Example:

    For i = 1 To 10
        list(i) = Int(100 * Rnd())
        If list(i) > max Then max = list(i)
        frmMaximum.Print "   i "; i; "  list(i) "; list(i)
    Next i

File operations: Open, Close, Input, Line, Output

File numbers do not require declarations

    Open filename For Input As #file number
    Line Input #file number, variable or array reference
    Close #file number

Example:

    Open infile For Input As #1
    nlines = 0
    Do While Not EOF(1) And nlines < 20 ' don't overrun array
        nlines = nlines + 1
        Line Input #1, lines(nlines)
	...
    Loop
    Close #1

also

    Open filename For Output As #file number

Example:

    Open outfile For Output As #2
    For i = 1 To nlines
        Print #2, lines(i)
    Next i
    Close #2

Print and Format

Semicolons separate arguments in Print. Arguments can be quoted strings, string-valued expressions, or variables. Variable values of numeric types etc. are converted to strings.

    Destination.Print arg; arg; ...

Example:

    Debug.Print nlines; lines(nlines)

Format does formatted output. For example "####.#" represent formatted (right justified) numbers.

    Format(expression, "format string")

Format calls often appear in Print arguments

Example:

    Debug.Print " xo "; Format(xo, "#####"); "  yo "; Format(yo, "#####"); _
        "  xi "; Format(xi, "#####"); "  yi "; Format(yi, "#####");

Other web pages

Back to top

Jon Jacky, jackyj@evergreen.edu