I have a pretty huge UDT but I'm now converting it as a Class Structure. However I'm having issues passing from/to the nested class properties.
Example:
Code:
Dim testDB() As Class1_Main
Private Sub Form_Load()
ReDim testDB(0)
Set testDB(0) = New Class1_Main
testDB(0).Item(0).xName = "test" 'TEST isn't being passed on?
MsgBox testDB(0).Item(0).xName 'EMPTY<<<<<<<<<<<<<
End
End Sub
I've attached the Simple Project.
Any help would be appreciated.
Re: [RESOLVED] Transfer UDT to Class [Nested Classes]
Yeah, I just thought your code through too, and LaVolpe is right on. The Clone function down in Class2_Items is very confusing.
The following line in your Form1 ...
Code:
testDB(0).Item(0).xName = "test" 'TEST isn't being passed on?
... the highlighted part is calling the Clone function. Remember that only the last piece of a statement is the Let/Set piece (the .xName piece). All other pieces will be Get pieces. Therefore, Item(0) will be a Get. And then, in your Clone procedure, you've got ...
Code:
Set Clone = New Class2_Items
... within that procedure. Therefore, this local Clone variable/object is uninstantiated once you're finished using it. Therefore, you've set the .xName property in a temporary (further nested, somewhat recursive) version of Class2_Items. Once the testDB(0).Item(0).xName = "test" statement completes, that Clone is destroyed, along with its .xName property.
If you start your program with F8 and just step through it, you'll see all of this.
I'm not clear on why you just didn't directly return the items in Class2 rather than trying to clone them. Are you trying to make a read-only version of them? That would be accomplished a bit differently.
The easiest way I could see to do this would be to just return the entire Class2 as a property of Class1. Something like the following ...
Code:
Friend Property Get Item(ByVal iIndex As Byte) As Class2_Items
' The item with iIndex must have been added before fetching with iIndex.
Set Item = xItem(iIndex)
End Property
Friend Property Set Item(ByVal iIndex As Byte, newItem As Class2_Items)
' The item with iIndex must have been added before being set with newItem.
Set xItem(iIndex) = newItem
End Property
... (aircode, I didn't test) that should just directly return the item with no cloning.
Good Luck,
Elroy
EDIT1: Ahhh, sorry, I took a bit to compose my post. Yeah, no need for your clone procedure.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: [RESOLVED] Transfer UDT to Class [Nested Classes]
Although this issue has been resolved, I cam across another interesting thing.
I have another property which is a ByteArray [xData]
But I can't seem to access it directly without making a temporary variable to dump the data.
Is that normal?
i.e
Code:
Private Sub Form_Load()
ReDim testDB(0)
Set testDB(0) = New Class1_Main
testDB(0).Item(0).xData = StrConv("test", vbFromUnicode)
Debug.Print testDB(0).Item(0).xData(2) '//Error here <<<<
End
End Sub