Microsoft KB Archive/204823

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

Article ID: 204823

Article Last Modified on 5/19/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q204823



IN THIS TASK

 * SUMMARY
 * Requirements
 * Step-By-Step Example
 * Verify That It Works
 * REFERENCES



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
 Start Visual Basic 6.0, and then create a new Standard EXE project.

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

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

By default, Class1 is added to the project. In the Properties window, change the class name to MyCalendar.  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  In the Project Explorer window, double-click Form1. Add a CommandButton to Form1.</li> In the Properties window, change the caption to Property Let.</li> Add another CommandButton to Form1.</li> In the Properties window, change the caption to Property Get.</li> In the Project Explorer window, right-click Form1, and then click View Code.</li>  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 </li> On the File menu, click Save Project to save the project.</li> On the Run menu, click Start to run the application.</li></ol>

back to the top

Verify That It Works
<ol>  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. </li> 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.</li></ol>

back to the top

<div class="references_section">