Click here to load reader

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

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Control Arrays, Records, and Record Arrays in 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. - PowerPoint PPT Presentation

Text of Control Arrays, Records, and Record Arrays in V.B

  • Control Arrays,Records, and Record Arraysin V.B.Week 10

  • Array example - sortingIn this example a simple set of inputs are set up.Clicking the top button allows data entered to be stored in the arrayThe middle one sorts the dataThe bottom button puts sorted data in the text boxes

  • Set Globals and Initialise dataConst cmin = 0Const cmax = 4Private numbers(cmin To cmax) As Integer declare data arraySub 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 NumbersSub 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 numbersSub 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 ArraysA control array is a group of controls that share the same:Nametypeevent 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 ArraysSub 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

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

  • Attaching code to control arrayAdd the following coding to the text1_keypress event attached to the FIRST box onlyPrivate 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 buttonPrivate Sub cmdAssign_Click () Dim i, error_count As Integer error_count = 0 For i = cmin To cmaxIf text1(i).Text = "" Then error_count = error_count + 1End 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 retrievalIn 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 ArraysCopy 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 farData arrays or control arraysData arrays multi-dimensionalReferenced collectively or individual elementsButSame 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:

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

    sTitleBetter I.T.sAuthorA.N. OthersPublisherCorgisISBN0552 1190 2iDate1976cPrice12.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

  • Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As CurrencyEnd Type

    sTitle sAuthor sPublisher sISBN iDate cPrice

  • Declaring user-defined variables :Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As CurrencyEnd Type

    Dim tBook As bookType

  • Declaring user-defined variables :Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As CurrencyEnd Type

    Dim tBook As bookType

  • 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 CurrencyEnd 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 CurrencyEnd 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 CurrencyEnd Type

    Dim tBook As bookType

    tBook.sAuthor = txtAuthor.TexttBook.iDate = 1978tBook.Publisher = Corgi

    lblAuthor.Caption = tBook.sAuthor

    Note : fields dont 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 bookTypeaLibrary(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 CurrencyEnd Type

    Private aLibrary (1 to 100) As bookType

    lblTitle.Caption = aLibrary(99).sTitle

  • 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

Search related