PDA

View Full Version : How to pass an Array in a Class Module?



HenryPotter
03-11-2009, 03:24 PM
Could anyone tell me how I could pass an array to a function where the array is in a class?

Public Property Get Holidays(Index As Integer) As Date
If Index > UBound(dTemp) Or Index < LBound(dTemp) Then
Holidays = ""
Else
Holidays = dTemp(Index)
End If
End Property
Public Property Let Holidays(Index As Integer, Value As Date)
dTemp(Index) = Value
End Property
Public Sub LoadHolidays(TempArray() As Date)
dTemp = TempArray
End Sub

Now in the module, I have a function that CountHolidays(H.Holidays as Variant) and it doesn't work.

Thanks for your help!

xld
03-11-2009, 03:38 PM
You are a bit short on details, but assuming that H is the class instance, H.Holidays should return an array of holidays in the class, as long as the Get Property of the class is right.

HenryPotter
03-11-2009, 07:27 PM
Yes H is the class instance.

Since I set the get property to get individual element in the array Holidays, I can't transfer the holidays array as a whole.

Public Property Get Holidays(Index As Integer) As Date
If Index > UBound(dTemp) Or Index < LBound(dTemp) Then
Holidays = ""
Else
Holidays = dTemp(Index)
End If
End Property

mikerickson
03-11-2009, 10:46 PM
The index doesn't need to be passed as an argument to the property.

If anArray is a property that returns an array , then

xVal.anArray is an array

xVal.anArray(6) is the 6th element of that array.

The 6 doesn't need to be passed to the class, it can be handled by "passing" it as a normal array index.

xld
03-12-2009, 01:33 AM
You don't seem to be returning an array, just a specific element within the array.

Can you be more specific on what the problem is and where it arisses.

HenryPotter
03-12-2009, 06:40 AM
Thanks Mike. Do I have to specify the Array as Variant in order to pass the array in a function?

Could we redefine the array as, say integer if I am calling for individual element?

Please see below: Would this work?



Dim myArray(1 to 10) As Integer

Property Get anArray(Optional index As integer) As Variant
If IsMissing(index) Then
anArray = myArray
Else
Dim anArray as integer
anArray = myArray(index)
End If
End Property
Private Sub Class_Initialize()
myArray = Array(, 11, 22, 33, 44)
End Sub


In the above example


Dim xVal as new Class1
xVal.anArray(1) = 22 (zero based array)
and
xVal.anArray = {11,22,33,44}

HenryPotter
03-12-2009, 11:30 AM
Looks like I can't redeclare a variable with a different type the way I described below.

Any other way that could achieve that? Thanks!

mikerickson
03-12-2009, 03:20 PM
Henry, you are responding to what I posted before thinking about the situation. (and editing my previous post)

What I am (now) suggesting is that your proprerty take no argument, and return an array.

in the class module
Property Get myArray() as Variant
myArray = Array(11,22,33,44)
End Property

In the normal module
Dim xVal as newClass 1
'...
MsgBox TypeName(xVal.myArray): ' returns Variant()
MsgBox TypeName(xVal.myArray(2)):' returns IntegerThe 2 in the last line is not an argument passed to the property routine, but the index of an array.

About type Variant, my Mac uses an earlier version of VB than Windows versions, so arrays returned from functions or properties have to be typed Variant. That has changed for Excel 2003 and 2007, but I'm not sure enough of that syntax to guide you.