1. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
CHAPTER 8
ARRAYS
Control Arrays
A control array is a group of controls that share the same name and type.
They also share the same event procedures.
Common uses for control arrays include menu controls and option button groupings.
Why Use Control Arrays?
Adding controls with control arrays uses fewer resources than simply adding multiple controls of the
same type to a form at design time. Control arrays are also useful if you want several controls to
share code. For example, if three option buttons are created as a control array, the same code is
executed regardless of which button was clicked.
Example:
Steps for creating Control Array of Option Button:
1. Place an option button control on the form.
2. Copy and paste the same option button control, a message box is displayed which ask
for creating control array of option button.
3. Click YES.
Private Sub optColor_Click(Index As Integer)
If Index = 0 Then
txtMessage.ForeColor = vbRed
ElseIf Index = 1 Then
txtMessage.ForeColor = vbGreen
ElseIf Index = 2 Then
txtMessage.ForeColor = vbBlue
End If
End Sub
Select Case
Visual Basic provides the Select Case structure as an alternative to If...Then...Else for selectively
executing one block of statements from among multiple blocks of statements.
A Select Case statement provides capability similar to the If...Then...Else statement, but it makes
code more readable when there are several choices.
1
2. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
A Select Case structure works with a single test expression that is evaluated once, at the top of the
structure.
Visual Basic then compares the result of this expression with the values for each Case in the
structure.
If there is a match, it executes the block of statements associated with that Case:
Select Case testexpression
[Case expressionlist1
[statementblock-1]]
[Case expressionlist2
[statementblock-2]]
.
.
.
[Case Else
[statementblock-n]]
End Select
Each expressionlist is a list of one or more values. If there is more than one value in a single list, the
values are separated by commas.
Each statementblock contains zero or more statements. If more than one Case matches the test
expression, only the statement block associated with the first matching Case will execute. Visual
Basic executes statements in the Case Else clause (which is optional) if none of the values in the
expression lists matches the test expression.
For example above program using Select Case:
Private Sub optColor_Click(Index As Integer)
Select Case Index
Case 0
txtMessage.ForeColor = vbRed
Case 1
txtMessage.ForeColor = vbGreen
Case 2
txtMessage.ForeColor = vbBlue
End Select
End Sub
2
3. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
Tips while using Select Case:
Constant [, Constant . . . ] Case 2, 4, 6
Constant to Constant Case 25 to 50
Is relational-operator Constant Case Is < 10
Single Dimensional Arrays
Arrays allow you to refer to a series of variables by the same name and to use a number (an index)
to tell them apart.
Arrays have both upper and lower bounds, and the elements of the array are contiguous within
those bounds, Because Visual Basic allocates space for each index number.
Arrays are also known as subscripted variables.
Syntax:
Dim ArrayName( [LowerSubscript To] UpperSubscript ) [As DataType]
Examples:
Dim arr(4) ‘arr is of type variant and its index ranges from 0 to 4.
Dim arrName(1 to 4) As String ‘arrName is of type String and its index ranges from 1 to 4.
Storing values in an Array using Index/Subscript
Syntax: arrname(index)=value
‘ arrAlphabets is String array containing alphabets.
Dim arrAlphabets(1 to 26) as String
arrAlphabets(1)=”A” ‘Storing String “A” at subscript/index 1
arrAlphabets(2)=”B” ‘Storing String “A” at subscript/index 2
arrAlphabets(3)=”C” ‘Storing String “A” at subscript/index 3
.
.
.
arrAlphabets(26)=”Z” ‘Storing String “A” at subscript/index 26
Getting values from an Array using Index/Subscript
Print arrAlphabets(26) ‘Prints Z on the form
Print arrAlphabets(1) ‘Prints A on the form
3
4. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
For Each Statement
Repeats a group of statements for each element in an array.
Syntax
For Each element In group
[statements]
[Exit For]
[statements]
Next [element]
The For...Each...Next statement syntax has these parts:
Part Description
element Required. Variable used to iterate through the elements of the collection
or array. For collections, element can only be a Variant variable, a generic
object variable, or any specific object variable. For arrays, element can
only be a Variant variable.
group Required. Name of an object collection or array (except an array of user-
defined types).
statements Optional. One or more statements that are executed on each item in
group.
Example:
Dim arrAlphabets(1 to 26) as String
arrAlphabets(1)=”A” ‘Storing String “A” at subscript/index 1
arrAlphabets(2)=”B” ‘Storing String “A” at subscript/index 2
.
.
arrAlphabets(26)=”Z” ‘Storing String “A” at subscript/index 26
Dim str as Variant
For each str in arrAlphabets
Print str
next
output: A
B
C
D
E
.
.
.
.
.
Z
Note: str should always be Variant.
4
5. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
User Defined Data Types
User-defined types are useful when you want to create a single variable that records several
related pieces of information.
You create a user-defined type with the Type statement, which must be placed in the
Declarations section of a module.
* User-defined types should be declared as Private in a form module and Public in Standard
Module.
For example:
Private Type MyDataType
-or-
Public Type MyDataType
For example, you could create a user-defined type that records information about a Student:
Private Type Student
Name as String
RollNo as Integer
Age as Integer
End Type
Declaring Variables of a User-Defined Type
You can declare local, private module-level, or public module-level variables of the same user-
defined type:
Dim s1 As Student ‘ Student variable s1
Dim s1(1 to 10) As Student ‘ Student Array s1 of 10 students
Example:
Private Type Student
name As String
rollno As Integer
End Type
Private Sub cmdOK_Click()
Dim s1 As Student
s1.name = txtname.Text
s1.rollno = Val(txtrollno.Text)
MsgBox "name of Student is : " & s1.name & " and Roll Number is : " & s1.rollno
End Sub
5
6. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
USING ARRAYS ELEMENTS FOR ACCUMULATORS
Dim arr(1 To 5) As Integer
Private Sub cmdSale_Click()
Dim level As Integer
Dim tickets As Integer
level = Val(cboLevel.Text)
tickets = Val(txtTickets.Text)
If level >= 1 And level <= 5 Then
arr(level) = arr(level) + tickets
End If
Cls
Print Space(5) & "level" & Space(5) & "tickets"
i=1
For i = 1 To 5
Print Space(5) & i & Space(5) & arr(i)
Next
End Sub
6
7. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)
TABLE LOOKUP
Private Type Stadium
level As String
ticket As Integer
End Type
Dim udtStadium(1 To 5) As Stadium
Private Sub Form_Load()
udtStadium(1).level = "A"
udtStadium(2).level = "B"
udtStadium(3).level = "C"
udtStadium(4).level = "D"
udtStadium(5).level = "E"
End Sub
Private Sub cmdSale_Click()
Dim found As Boolean
Dim nooftickets As Integer
Dim userlevel As String
found = False
userlevel = cboLevel.Text
nooftickets = Val(txtTickets.Text)
i=1
Do While found = False Or i <= 5
If userlevel = udtStadium(i).level Then
udtStadium(i).ticket = udtStadium(i).ticket + nooftickets
found = True
End If
i=i+1
Loop
If found = False Then
MsgBox "Invalid Stadium Level"
End If
Cls
Print Space(5) & "level" & Space(5) & "tickets"
i=1
For i = 1 To 5
Print Space(5) & udtStadium(i).level & Space(5) & udtStadium(i).ticket
Next
End Sub
7