Transcript VB_Arrays

Arrays and others
Annoucement

Today’s office hour move to Friday 1:00PM to
3:00PM

Today
 Call by reference and call by value
 Variable scopes
 Arrays
 Debugging
Call by reference

Passing the whole variable memory block to the
sub procedure
If inside is changed, outside will be changed too.

How:



Private Sub Add(num1 as integer, num2
as integer)
Call Add(x,y)
Call by value



Only copy the value of the variable to the sub
procedure.
Inside changes will not affect the outside variables.
How:

Call Add((x),y)
Call by reference and call by value also apply to
the function procedure.
When to use.



Private Sub Add(num1 as integer, num2
as integer)
Scope of a variable
Local scope.
 Variable declared in a sub procedure
 It only exists in this sub procedure.
 Form-level scope.
 Variable declared out of any sub
procedure.
 Any sub procedure can read it.

Arrays






Array is a special type of variable
Regular Variables hold one value
Arrays hold may values - subscripts
X(1), X(2), X(3), X(4), X(5)
Arrays are like subscripts – refer to different
values stored in the same variable
Pp. 175-187
What Arrays Do

Suppose Data is an array.
Data(1)
Data(2)
Data(3)
Data(4)
Data(5)
First
value
Second
value
Third
value
Fourth
value
Fifth
value
22.49
13.42
22.17
64.11
7.33
Declaring Arrays

Use Dim key word to declare arrays, just as
what we do for variables.

Dim Data(1 to 50) As Single
 Dim Species(1 to 4) As String
Accessing values in an array with subscript.
 Data(44) = 22.5;
 Species(1) = “ACTGACTCGTAACGT”

Red Number is INDEX

Matrices: 2-dimensional Arrays



Declaration
 Dim Data(1 to 50, 1 to 20) As
Single
Accessing
 Data(2,4)=100
The range of the subscript can be between any
integers.
 Dim WeekendMorning(6 to 7, 7 to
12) As String
Passing array to a sub procedure

Array can be passed as an argument to sub
procedures of function procedures.




Private Sub ProcName(ArrayName() as
Integer)
Call ProcName(Array1)
Array can only be called by reference.
Function Ubound(ArrayName,2)
ReDim
Use ReDim when declaring an array size
based on a variable
 Also called dynamic array
 User tells you there are 50 values (count =
50)
 ReDim Values(1 to count) as Single

Decimal to Binary calculator
Start
Flow chart
If number No
Equal to 1 or
0
Get a
number
Yes
End
Divide the
Number
By 2
No
Output
number
Print
The Remainder
to the left of
previous remains
Is the quotient
Equal to 1?
Print 1 To the
left of
Previous remainders
Yes
Convert the first decision to code
IF number <> 0 OR number <> 1 THEN
Do the conversion part
END IF
Call OutputNumber(number)
Code fragment for the loop
Do While quotient <> 1
quotient = number \ 2
reminder = number mod 2
number = quotient
call PrintReminder()
Loop
Problems
Print 1 To the
left of
Previous remainders
So far we have already solved the
Decision structure and loop structure.
 Problems not solved.
 All previous reminders need to be
stored.
 How many?

Steps



First we need determine how many places in the
converted binary number
 Int(Log(Dec) / Log(2)) + 1
Second, declare a dynamic array to store all the
output numbers.
 ReDim Bin(1 to binplaces) as
String
In the do while loop, put reminders of each step in
the Bin array. Beware of the order
Output function
Output
number
Private Sub Output(rems() As
String)
‘Print the binary array in the
picOutput picture box
End Sub
Debugging tools in VB

Break points
Debugging tools in VB(2)

Immediate Window
Debugging tools in VB(3)

Watch window and local window
Friday

Do the program that convert 10 base
value to binary.