PDA

View Full Version : Solved: passing an array to a class



BrianMH
04-19-2011, 08:00 AM
Hi,

I am looking at creating my first class. Instead of assigning each property individually is there a way for me to pass it an array and have those properties set from the array.

So instead of
myclass.property1 = "asdf"
myclass.property2 = "safd"
myclass.property3 = 1234

I just pass it the array of "asdf", "safd", 1234 and it assigns those properties automatically?

Thanks

Bob Phillips
04-19-2011, 08:15 AM
You could do something like,



'caller code

myclass.SetArray ("asdf", "safd", 1234")


'class code

Private mProperty1 As String
Private mProperty2 As String
Private mProperty3 As Long

Public Property Get Property1 As String
Property1 = mProperty1
End Property

Public Property Get Property2 As String
Property2 = mProperty2
End Property

Public Property Get Property3 As Long
Property3 = mProperty3
End Property

Public Function SetArray(ary As Variant)
mProperty1 = ary(1)
mProperty2 = ary(2)
mProperty3 = ary(3)
End Function

although I wouldn't, I would stick with the normal approach

BrianMH
04-19-2011, 08:41 AM
although I wouldn't, I would stick with the normal approach

Any particular reason?

Bob Phillips
04-19-2011, 09:07 AM
Because it just looks and feels more 'right' to me, properties are individual attributes, so pass them individually.