View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Peter T[_7_] Peter T[_7_] is offline
external usenet poster
 
Posts: 162
Default Problem getting a pointer to a User Defined Type (UDT)

"Desmond Walsh" wrote in message
=====================

I have a data structure consisting of an array of User Defined Types (UDF) -
called Udta say. Each Udta contains 4 members that are also UDT's - called
Udtb say. I wrote a function to return a pointer to a specific Udtb. But to
my surprise, the pointer creates a new instance of Udtb although it inherits
from the Udtb I was trying to point to.

I have confirmed this behaviour by monitoring the variables in Local Window.
I also display the pointer values using VarPtr.

My question. Is it possible to get a pointer to a specific Udtb as I am
trying to do. It seems to me that I want 2 pointers pointing at the same
UDF. I would prefer to stay with UDT's and not use classes.

The code is vba in Excel 2010.
=======================



I know it's been a while since you posted but I don't follow what you are
trying to do or what the problem is.

You say your function to return the pointer to a particular Udtb creates a
new instance of your Udtb and a new pointer. Looking at the code you posted
later that's because you assign the Udtb passed to the function as the
return value of the function. That creates a copy of the Udtb which will
indeed have a new pointer

Your code, where GetUdtPointer is the function name declared as Udtb and
marray(2).amember1 (passed to the function) is also a Udtb -
GetUdtPointer = marray(2).amember1
that's like doing myUDT2 = myUDT1, makes a distinct copy

When the function returns and you assign the function return to a different
variable, the contents will be copied into this variable which will have yet
another different pointer, then the copy and pointer assigned to the
function will fall out of scope.

Peter T