Transcript

1.
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI) CHAPTER 8 ARRAYSControl ArraysA 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 thesame type to a form at design time. Control arrays are also useful if you want several controls toshare code. For example, if three option buttons are created as a control array, the same code isexecuted 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 SubSelect CaseVisual Basic provides the Select Case structure as an alternative to If...Then...Else for selectivelyexecuting 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 makescode 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 thestructure. Visual Basic then compares the result of this expression with the values for each Case in thestructure.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 SelectEach expressionlist is a list of one or more values. If there is more than one value in a single list, thevalues are separated by commas. Each statementblock contains zero or more statements. If more than one Case matches the testexpression, only the statement block associated with the first matching Case will execute. VisualBasic executes statements in the Case Else clause (which is optional) if none of the values in theexpression 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 < 10Single Dimensional ArraysArrays 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 withinthose 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 26Getting 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 StatementRepeats 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 Descriptionelement 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 nextoutput: A B C D E . . . . . ZNote: str should always be Variant. 4

5.
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)User Defined Data TypesUser-defined types are useful when you want to create a single variable that records severalrelated pieces of information.You create a user-defined type with the Type statement, which must be placed in theDeclarations section of a module.* User-defined types should be declared as Private in a form module and Public in StandardModule. For example: Private Type MyDataType -or- Public Type MyDataTypeFor 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 TypeDeclaring Variables of a User-Defined TypeYou can declare local, private module-level, or public module-level variables of the same user-defined type:Dim s1 As Student ‘ Student variable s1Dim s1(1 to 10) As Student ‘ Student Array s1 of 10 studentsExample: 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