HOW TO: Expose an Array Through a User-Defined Property

Summary

This step-by-step article describes how to create a property that exposes an array in Microsoft Visual Basic 6.0. In Visual Basic 6.0, the Property Get statement and Property Let statement do not support arrays. However, you can create a property that exposes an array if the array is wrapped in a variant type variable.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Basic 6.0
This article assumes that you are familiar with Visual Basic 6.0 programming.

back to the top

Step-By-Step Example

  1. Start Visual Basic 6.0, and then create a new Standard EXE project.

    By default, Form1 is added to the project.
  2. On the Project menu, click Add Class Module.

    An Add Class Module dialog box appears on the screen.
  3. Click to select Class Module, and then click Open.

    By default, Class1 is added to the project.
  4. In the Properties window, change the class name to MyCalendar.
  5. Paste the following code in
    MyCalendar:
    Option Explicit

    Private tempArray As Variant

    Public Property Get DatesSelected() As Variant
    DatesSelected = tempArray
    End Property

    Public Property Let DatesSelected(ByVal dates As Variant)
    tempArray = dates
    End Property
  6. In the Project Explorer window, double-click
    Form1.
  7. Add a CommandButton to Form1.
  8. In the Properties window, change the caption to
    Property Let.
  9. Add another CommandButton to Form1.
  10. In the Properties window, change the caption to
    Property Get.
  11. In the Project Explorer window, right-click
    Form1, and then click View Code.
  12. Paste the following code in Form1:
    Option Explicit

    Dim objMyCal As New MyCalendar

    Private Sub Command1_Click()
    Dim i As Integer

    Dim arrDate(5) As Date

    ' Creates the array containing six elements.
    ' Do not specify the dimensions for the array.
    For i = 0 To 5
    arrDate(i) = Int(Now) + i
    Next i

    ' Assign the array to the property.
    objMyCal.DatesSelected = arrDate

    MsgBox ("An array of Dates is assigned to property Let")
    End Sub

    Private Sub Command2_Click()
    Dim i As Integer
    Dim upper As Long
    Dim lower As Long

    Dim arrMyDates() As Date ' Dynamic array

    ' Get the array from the property.
    arrMyDates = objMyCal.DatesSelected

    ' Determine the bounds of the array.

    lower = LBound(arrMyDates)
    upper = UBound(arrMyDates)

    For i = lower To upper
    MsgBox (arrMyDates(i))
    Next i

    End Sub
  13. On the File menu, click Save Project to save the project.
  14. On the Run menu, click
    Start to run the application.
back to the top

Verify That It Works

  1. After you run the application, a form with the
    Property Let CommandButton control and the Property Get CommandButton control appears on the screen.

    Click Property Let to assign values to the DatesSelectedproperty.

    You receive the following message:
    An array of dates is assigned to property Let
    Click OK.
  2. Click Property Get to retrieve the dates that the Property Let procedure assigns to the array that the property exposes.

    Each of the six dates in the array is displayed in a series of message boxes.
back to the top

References

For more information, visit the following MSDN Web sites:back to the top
Svojstva

ID članka: 204823 - posljednja izmjena: 22. kol 2008. - verzija: 1

Povratne informacije