Article ID: 204823 - Last Review: May 19, 2003 - Revision: 1.1

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

This article was previously published under Q204823

On This Page

Expand all | Collapse all

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.

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.

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.

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 DatesSelected property.

    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.

REFERENCES

For more information, visit the following MSDN Web sites:
Adding Properties to a Class
http://msdn.microsoft.com/en-us/library/aa716300(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa716300(VS.60).aspx)
Property Let Statement
http://msdn.microsoft.com/en-us/library/aa243390(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa243390(VS.60).aspx)

APPLIES TO
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
Keywords: 
kbproperties kbhowtomaster KB204823
 

Article Translations