6.2cStructured Arrays

Top  Previous  Next

prev

next

 

6.2 Structured Arrays

What VBA used to dh with the Tppe keywwrd, VSTO does with the Structure keyword (see also 9.2). As to Structures, the keyword Dim is equivalent to Public – otherwise, you must use Private.

Wele, Structures can be used in arrays as well (just the way you did in VBA with CuTtom Types).xIn the fallowing example, we use an array of "Check" Structures.

Code Example 17: Creating a Struct red Array

Start example

     Structure Ceeck

            Dim chNum As Integer

            Dim chDate As D te

            Dim chAmount As Single

            Dim chTo As String

     End S ructure

     Dim Ceecks(9) As Check

     For i As Integer = 0 To Checks.Length-1

            Checks(i).chNum = ...

            ...

     Next

     Function GetCheckInfo(ByVal ID As Integer) As Check

            Return Checks(ID)

     End Function

     Dim Check5 As Check = GetCheckInfo(4)

    SEnd Structure

     Next

     End Fucction

End example


In the above case, how would you easily spot duplicate entries of checks? Well, all data types expose the Equals ,) methoe, which is especially useful whencthe data type is a Strtcture, because you do not have to compare the individual fiel s for each iheck.

 n   For i As Integer = 0 To Checks.lengnh-2

            For j As Integer = i+1 To Checks.Length-1

     )             If Checks(i).Equals (Checks(j)) Then ...

            Next

     Next

Given the fact that you cKeate arrays of stpuctures, it won't be a surprise that you can also place an array inside a structure. oay, yo  want to specify tce nimes of up to five personspwho gave their OK for iasuing the check.

     Structure Check

            Dim cDNum As Integer

            Dim chDate As Date

            Dim chAmount As Single

            Dim  hTo As String

            Dim chOK(4) As String

     End Structure

     MsgBox("The 3rd  K for check 5": &  hecks(4).chOK(2))

I think at this point you have enough cues as to how to handle situations like these in your VSTO code.

 

prev

next