Array of custom data type in class

chambois
09-08-2009, 09:08 PM
Hey there,

I have a class ('MyClass') that has an array ('myArr') of a custom data type. When I try to set the value of one of the variables in the custom data type, it doesn't get set (or saved). The layout for these three pieces of the puzzle is the following:

MyClass

''' MyClass
Private pMyArr() As customType

' return whole array
Public Property Get myArrs() As customType
myArrs = pMyArr
End Property

' return element of array
Public Property Get myArr(Index As Integer) As customType
myArr = pMyArr(Index)
End Property

Public Property Let myArr(index As Integer, Value As customType)
pMyArr(index) = Value
End Property


CustomType

''' CustomType
Public Type customType
myVar_1 As String
myVar_2 As String
End Type
'--------


Test

''' test
Dim myObj As MyClass
Set myObj = New MyClass
...
For i = 0 To 4
myObj.myArr(i).myVar_1 = "Hello" ' doesn't set
myObj.myArr(i).myVar_2 = "world" ' doesn't set
Next i


I was wondering if anyone can tell me why that code doesn't work and/or if there is a better way to go about it.

Any help would be much appreciated

With Thanks,
Matt

kassyopeia
09-08-2009, 09:40 PM
It's a by-value/by-reference problem.

((myObj.myArr(i)).myVar_1) = "Hello"

(brackets for clarification)

The inner bit invokes the Property Get of the class, which assigns myArr = pMyArr(Index), meaning it copies the contents of the array item into the variable myArr. The outer bit does what you intend, it references a type member, instead of once again copying that member to a new variable.

So, the assignment successfully puts "Hello" into the type, but doesn't put the type into the array, operating on the temporary copy created in the Property Get instead. And as that copy is not even assigned to anything in the calling scope, it in effect expires at the end of the assignment line.

There is no (straightforward) way to return a reference to a type, so the best solution is to get the temp copy, modify it, then put it back explicitly:

Dim myArr As customType
myArr = myObj.myArr(i)
myArr.myVar_1 = "Hello"
myObj.myArr(i) = myArr

The last line now invokes the Property Set, which contains the inverse assignment pMyArr(index) = Value, meaning it puts the temp copy back into the array.

ps: Incidentally, the correct declaration for the array-property should be

Public Property Get myArrs() As customType()

The trailing empty brackets make it an array of customTypes, like the non-trailing ones in Private pMyArr() As customType. Furthermore, be advised that returning arrays can under certain conditions create memory leaks in VBA. I'm not sure if this issue also applies to VB6, but you should probably google it just in case. Details here.

chambois
09-08-2009, 10:11 PM
Wonderful! Thanks a lot for your help - it works now :) Thanks also for the fast and detailed response too

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum