Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Arrays, Records, and Record Arrays in V.B. Week 10.

Similar presentations


Presentation on theme: "Control Arrays, Records, and Record Arrays in V.B. Week 10."— Presentation transcript:

1 Control Arrays, Records, and Record Arrays in V.B. Week 10

2 Array example - sorting In this example a simple set of inputs are set up. Clicking the top button allows data entered to be stored in the array The middle one sorts the data The bottom button puts sorted data in the text boxes

3 Set Globals and Initialise data Const cmin = 0 Const cmax = 4 Private numbers(cmin To cmax) As Integer ‘declare data array’ Sub Form_Load () Dim i As Integer Rem initialise array contents For i = cmin To cmax numbers(i) = 0 Next i Rem initialise text boxes text1 = numbers(0) text2 = numbers(1) text3 = numbers(2) text4 = numbers(3) text5 = numbers(4) End Sub

4 Store Numbers Sub cmdAssign_Click () If (text1.Text = "") Or (text2.Text = "") Or (text3.Text = "") Or (text4.Text = "") Or (text5.Text = "") Then Beep MsgBox ("a zero length string is present") Else numbers(0) = CInt(text1.Text) numbers(1) = CInt(text2.Text) numbers(2) = CInt(text3.Text) numbers(3) = CInt(text4.Text) numbers(4) = CInt(text5.Text) End If End Sub

5 Sort Numbers Sub cmdRearrange_Click () Dim i As Integer Dim pass As Integer Dim temp As Integer Dim NoSwitches As Integer pass = 0 Do pass = pass + 1 NoSwitches = 1 For i = cmin To (cmax - pass) If numbers(i) > numbers(i + 1) Then NoSwitches = 0 temp = numbers(i) numbers(i) = numbers(i + 1) numbers(i + 1) = temp End If Next i Loop Until NoSwitches = 1 End Sub

6 Redisplay numbers Sub cmdRetrieve_Click () label1.Caption = numbers(0) label2.Caption = numbers(1) label3.Caption = numbers(2) label4.Caption = numbers(3) label5.Caption = numbers(4) End Sub

7 Control Arrays A control array is a group of controls that share the same: Name type event procedures. It can have from one to as many elements as your system allows. They are useful if you want several controls to share the code or if you want them to be created at run-time.

8 Control Arrays Sub Text1_KeyPress (Index As Integer, Keyascii As Integer) If (Keyascii Asc("9"))) Then Keyascii = 0 Beep MsgBox ("number must be between 0 and 9") End If End Sub

9 Previous example using a control array This time cut and paste the text box and label. This creates two control arrays

10 Attaching code to control array Add the following coding to the text1_keypress event attached to the FIRST box only Private Sub Text1_KeyPress (Index As Integer, Keyascii As Integer) If (Keyascii Asc("9"))) Then Keyascii = 0 Beep MsgBox ("number must be between 0 and 9") End If End Sub On analysis you will see that the same code appears in all the textboxes

11 Amend assign button Private Sub cmdAssign_Click () Dim i, error_count As Integer error_count = 0 For i = cmin To cmax If text1(i).Text = "" Then error_count = error_count + 1 End If Next i If error_count > 0 Then Beep MsgBox (error_count & " zero length string(s) present") Else For i = cmin To cmax numbers(i) = CInt(text1(i).Text) Next i End If End Sub

12 Simplify data access and retrieval In the retrieve and display button event replace the code with the following : Private Sub cmdRetrieve_Click () For i = cmin To cmax label1(i).Caption = numbers(i) Next i End Sub Lastly in the form load procedure replace the code with the following code : Private Sub Form_Load () Dim i As Integer Rem initialise array contents and text boxes For i = cmin To cmax numbers(i) = 0 text1(i) = numbers(i) label1(i) = "" Next i End Sub

13 Control Arrays ‘Copy and paste’ to create Create controls at run-time using load method Match to data arrays Can be used to create a Multiple Document Interface Create positional based displays such as games and panels (e.g. calculator number pad)

14 Knowledge of arrays so far… Data arrays or control arrays Data arrays multi-dimensional Referenced collectively or individual elements But… Same type only Simple data Cannot store composite data relating to same entity Are a fixed size (this is addressed by using redim and preserve – further reading !!) You should ignore my lack of usage of the hungarian notation when naming controls and variables !!!

15 Consider the data associated with a book: FieldTypeExample TitleString Better I.T. AuthorString A.N.Other PublisherString Corgi ISBNString 0552 1190 2 DateInteger 1976 PriceCurrency £12.95

16 Problems with variables: We could use 5 separate variables to hold the data for 1 book : sTitle Better I.T. sAuthor A.N. Other sPublisher Corgi sISBN 0552 1190 2 iDate 1976 cPrice £12.95 but this would not show that the data items are logically related to each other

17 A User-defined type groups related data items together specifies the format of a record, which may have several fields does not hold data

18 An example : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Fields A Record structure

19 Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type sTitle sAuthor sPublisher sISBN iDate cPrice bookType These statements define a type with this structure

20 Declaring user-defined variables : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType t for user-defined type The user-defined type DOES NOT CONTAIN DATA. It is a template for a variable, which is defined just like any other variable

21 Declaring user-defined variables : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType MUST be DEFINED IN THE MODULE Variables declared either in the module or in an Event Procedure

22 Using user-defined variables (or records) : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType Fields are accessed using the syntax (e.g.): tBook.sAuthor

23 A fuller example : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType tBook.sAuthor = txtAuthor.Text tBook.iDate = 1978 tBook.Publisher = “Corgi” lblAuthor.Caption = tBook.sAuthor

24 Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType tBook.sAuthor = txtAuthor.Text tBook.iDate = 1978 tBook.Publisher = “Corgi” lblAuthor.Caption = tBook.sAuthor Note : fields don’t have to be used in same order as defined not necessary to always use them all

25 Arrays of records: Example : Private aLibrary (1 to 100) As bookType Better I.T.A.N.OtherCorgi0 552 11190 2 197612.95 SnookerF.BloggsMacmillan0 516 12380 3 19214.32 :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: Flight of Fancy P.TaylorFontana0 987 43278 5 19997.99 WoodyW.AllenArden0 768 65908 3 198014.89 aLibrary(1) aLibrary(2) aLibrary(99) aLibrary(100)

26 Accessing arrays of user-defined types An example syntax: Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Private aLibrary (1 to 100) As bookType lblTitle.Caption = aLibrary(99).sTitle array name field name array index

27 Summarising ….

28 User-defined types: are usually called “records” group related data items (or “fields”) together can have fields of different data types are usually used in conjunction with Files


Download ppt "Control Arrays, Records, and Record Arrays in V.B. Week 10."

Similar presentations


Ads by Google