'Class: MyClassOption ExplicitPublic Key As StringPrivate mvarMyClassProperty As LongPublic Property Let MyClassProperty(ByVal vData As Long) mvarMyClassProperty = vDataEnd PropertyPublic Property Get MyClassProperty() As Long MyClassProperty = mvarMyClassPropertyEnd Property
'Class: MyCollectionOption ExplicitPrivate mCol As CollectionPublic Function Add(Key As String, MyClassProperty As Long, Optional sKey As String) As MyClass 'create a new object Dim objNewMember As MyClass Set objNewMember = New MyClass 'set the properties passed into the method objNewMember.Key = Key objNewMember.MyClassProperty = MyClassProperty If Len(sKey) = 0 Then mCol.Add objNewMember Else mCol.Add objNewMember, sKey End If 'return the object created Set Add = objNewMember Set objNewMember = NothingEnd FunctionPublic Property Get Item(vntIndexKey As Variant) As MyClass Set Item = mCol(vntIndexKey)End PropertyPublic Property Get Count() As Long Count = mCol.CountEnd PropertyPublic Sub Remove(vntIndexKey As Variant) mCol.Remove vntIndexKeyEnd SubPublic Property Get NewEnum() As IUnknown Set NewEnum = mCol.[_NewEnum]End PropertyPrivate Sub Class_Initialize() Set mCol = New CollectionEnd SubPrivate Sub Class_Terminate() Set mCol = NothingEnd Sub
Basically, VB classes (including the collection) and UDTs don't mix. My guess is that they were going to get rid of the UDTs, except they realised that in many ways they are better than the class system they had devised. (Things like file saving, speed and simplicity)
When order isn't important, you can beat a collection hands down for speed (and code cleanliness). I haven't done tests for when the order within the container is needed to be sustained, but I haven't even thought about it.
What you *can* do, if you are in a position where you need to use UDTs, is something like this:
Sub PutUDTInArray(MyUDT As udtBlah, PutIn() As Byte) Redim PutIn(Len(MyUDT) - 1) 'Assumes 0 based array CopyMemory ByVal VarPtr(PutIn(0)), MyUDT.FirstElement, Len(MyUDT)End SubFunction GetUDTFromArray(GetFrom() As Byte) As udtBlah CopyMemory ByVal GetUDTFromArray.FirstElement, VarPtr(PutIn(0)), Len(GetUDTFromArray)End Function
But, you have to be careful, because if you use that with a udt with indirect data types (variable length strings, dynamic arrays), then copying just that memory will not be enough, but you have resize the destination and copy across there too. Could turn into a lot more work than you want.
So, probably the best solution is to just make a dynamic array with a wrapper around it so it acts like a collection. It will probably be faster, since the VB collection is one of those great "lets use Variants for everything since they are flexible" type things. For games, this is what I do, and performance is fine, but when I do it, I don't need to maintain order.
btw, Magmai, are you Thraka at another message board?
Trying is the first step towards failure. Edit: Switched from codeblock? tags to source tags... the tag I always forget
. Oh, and I might as well try and squeeze this in here, because I meant to say that squashing the udt into a byte array is ugly... although I'm sure you noticed... and I don't know the copymemory api call off the top of my head.
Edited by - ragonastick on January 4, 2002 5:26:17 AM