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

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

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Control Arrays,Records,

and Record Arraysin V.B.

Week 10

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

Set Globals and Initialise data

Const cmin = 0Const cmax = 4Private numbers(cmin To cmax) As Integer ‘declare data array’ Sub Form_Load ()Dim i As IntegerRem initialise array contentsFor i = cmin To cmax numbers(i) = 0Next iRem initialise text boxes text1 = numbers(0) text2 = numbers(1) text3 = numbers(2) text4 = numbers(3) text5 = numbers(4)End Sub

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 IfEnd Sub

Sort NumbersSub cmdRearrange_Click ()Dim i As IntegerDim pass As IntegerDim temp As IntegerDim NoSwitches As Integerpass = 0Do 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 iLoop Until NoSwitches = 1End Sub

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

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.

Control Arrays

Sub Text1_KeyPress (Index As Integer, Keyascii As Integer)

If (Keyascii < Asc("0") Or (Keyascii > Asc("9"))) Then

Keyascii = 0

Beep

MsgBox ("number must be between 0 and 9")

End If

End Sub

Previous example using a control array

This time cut and paste the text box and label.

This creates two control arrays

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("0") Or (Keyascii > Asc("9"))) Then Keyascii = 0 Beep MsgBox ("number must be between 0 and 9")End IfEnd Sub

On analysis you will see that the same code appears in all the textboxes

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 IfEnd Sub

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 iEnd SubLastly in the form load procedure replace the code with the following code :Private Sub Form_Load ()Dim i As IntegerRem initialise array contents and text boxesFor i = cmin To cmax numbers(i) = 0 text1(i) = numbers(i) label1(i) = ""Next iEnd Sub

Control Arrays

‘Copy and paste’ to createCreate controls at run-time using load methodMatch to data arraysCan be used to create a Multiple Document InterfaceCreate positional based displays such as games and panels (e.g. calculator number pad)

Knowledge of arrays so far…

Data arrays or control arraysData arrays multi-dimensionalReferenced collectively or individual elements

But…Same type onlySimple dataCannot store composite data relating to same entityAre 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 !!!

Consider the data associated with a book:

Field Type Example

Title String Better I.T.Author String A.N.OtherPublisher String CorgiISBN String 0552

1190 2Date Integer 1976Price Currency £12.95

Problems with variables:

We could use 5 separate variables to hold the data for 1 book :

sTitle Better I.T.sAuthor A.N. OthersPublisher CorgisISBN 0552 1190 2iDate 1976cPrice £12.95

but this would not show that the data items are

logically related to each other

A User-defined type

groups related data items together

specifies the format of a record, which may have several fields

does not hold data

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

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

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 typeDOES NOT CONTAIN DATA.

It is a template for a variable,which is defined

just like any other variable

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 eitherin the module

orin an Event Procedure

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

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.TexttBook.iDate = 1978tBook.Publisher = “Corgi”

lblAuthor.Caption = tBook.sAuthor

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.TexttBook.iDate = 1978tBook.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

Arrays of records:Example :

Private aLibrary (1 to 100) As bookType

Better I.T.

A.N.Other

Corgi 0 552 11190 2

1976 12.95

Snooker F.Bloggs Macmillan

0 516 12380 3

1921 4.32

::

::

::

::

::

::

::

::

::

::

::

::

Flight of Fancy

P.Taylor Fontana 0 987 43278 5

1999 7.99

Woody W.Allen Arden 0 768 65908 3

1980 14.89

aLibrary(1)

aLibrary(2)

aLibrary(99)

aLibrary(100)

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

Summarising ….

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