Click to See Complete Forum and Search --> : TypeCast in VB??


Trent Fryar
January 27th, 2000, 01:20 AM
Referring to the Code below, how would you retrieve the structure of 'a' back from its memory address stored in the the ItemData Property of a ListBox (or any other control.) CopyMemory and MoveMemory will not work because of the strings/arrays of string stored in the structure. Is there a way to TypeCast a value in VB? Or is there any other way to store a structure in the ItemData
Property of a ListBox that is retrievable?

Thanks, Trent

Dim tArr() As String

Type t
i As Integer
s As String
arr As tArr
End Type

Dim a As t, b As t

Private Function SomeFunc
Dim lb As ListBox, nSize As Long
Dim SomeArray() As String

Redim SomeArray(10)

' Input Dummy Values
a.i = 1
a.s = "One"
a.arr = SomeArray

lb.AddItem vbNullString ' Add a blank item to the List Box
lb.ItemData(0) = VarPtr(a) ' Add a pointer to the Structure a to the ItemData proprerty using varPtr

b = lb.ItemData(0) ' ? Incompatible data types
nSize = len (b) ' Get the size of the data structure.
b = CopyMemory(b, lb.ItemData(0), nSize) ' Takes this as a literal 'Long' instead of a structure

End Function

Crazy D @ Work
January 27th, 2000, 05:07 AM
Don't know if it helps.... but in a subclass dll, I use Objptr to save a pointer to the class that is maintaining the subclass, I use copymemory in the subclass function to restore the pointer to that class to a class of that type so I can call a specified function in that class (which will raise an event) and that works perfectly... maybe you can change the type to a class and try it with that. (hope this makes sense....)

Crazy D @ Work :-)