PDA

View Full Version : Solved: Passing & Returning array to/from Property Get and Let



みどり
09-10-2007, 11:06 PM
Hello Ladies and Gentlemen, please give assistance in the following. I have a Class Module, it has some properties (and declared internal variables, that correspond to these properties). One of these internal variables is an array:
Dim A#()
I want to refer it from a property, as I do with a usual variable:
Property Get X() as Double()
X = A
End Property
Property Let X(newX() as Double)
A = newX
End Property

It doesn?t work this way. It says ?can?t assign to array? when I try to either read the property or write it. Of course, I can read and write them element-by-element, passing the item number as a parameter, and looping through property assignment inside the program code. But I want to make it a nicer way, passing array to property and returning it from property.

IanAgain
09-10-2007, 11:39 PM
I had similar problems but I could not defeat the system. In the end I had to compromise with functions that passed in data one at a time to an array that was internal to the class. If I wanted the data in the array then I had to reference the data position (like a normal array). The code is:
'**********************
Public Property Let sCriterial(iVal As Integer, sCriterialIn As String)

ReDim Preserve sCriterialArray(1 To iVal) 'iCriterialLength

iCriterialLength = UBound(sCriterialArray)
sCriterialArray(iVal) = sCriterialIn 'iCriterialLength

End Property
'************************

Public Property Get sCriterial(iVal As Integer) As String
' used to pass arrays out of class module
sCriterial = sCriterialArray(iVal)
End Property
'*************************
I had to supply the position for the Property let array as well. This works fine for a loop situation but you must be careful for applicaitons outside a loop.

Bob Phillips
09-11-2007, 01:09 AM
This works for me



'Class
Private maItems

Public Property Get ClassItems()
ClassItems = maItems
End Property

Public Property Let ClassItems(ary)
maItems = ary
maItems(2) = 99
End Property


As a simple example of usage



Dim cls As Class1
Dim ary


Set cls = New Class1
ary = [{1,2,3}]
cls.ClassItems = ary
MsgBox ary(2)
ary = cls.ClassItems
MsgBox ary(2)

みどり
09-11-2007, 06:26 AM
IanAgain, xld, thank you!

I think I will use a sub to assign an array to the internal array. Passing arrays through subs is convenient. And concerning returning array without item number, i will just avoid it. ^_^

My class will look like this:
'Class
Dim A#()

Sub AssingToA(newA#())
A = newA
End Sub
xld, the example you've provided shows the true power of Variant data type. I'm not much familiar with it, maybe I should pay more attention to possibilities it gives. Thank you for the idea!

Bob Phillips
09-11-2007, 06:29 AM
The big advantage of an array, within a class module, is that as well as being able to pass the arrau wholesale, it is just as simple to get/set a single item within the array. So, although item number may not be appropriate for you here, don't just forget it, it is extremely useful.