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
- 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.
- In the Properties window, change the caption to
Property Let.
- Add another CommandButton to Form1.
- In the Properties window, change the caption to
Property Get.
- In the Project Explorer window, right-click
Form1, and then click View Code.
- 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
- On the File menu, click Save
Project to save the project.
- On the Run menu, click
Start to run the application.
Verify That It Works
- 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. - 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.
For more information,
visit the following MSDN Web sites: