Use ObjPtr(Me) to return the Name of a Custom Class Instance?

Viewed 2805

I understand that ObjPtr will return the address of an object in memory and that it points to a structure called IUNKNOWN and that there is some kind of Interface definition encoded in that to expose the Object structure, but I couldn't figure out how to determine the interfaces for a VBA Custom Class Object and how to use that to return the Name property of an Object.

It's more "nice to have" than essential, but I just want to know the name of an object instance at run time so that I can include it in my trace messages.

Can anyone explain how to do this or, better yet direct me to a reference so I can figure it out?

EDIT

To re-state my aim:

To make a custom class objects that is able to figure out the name of its particular instance.

For example

Dim oObject1 as Class1, oObject2 as Class1
Set oObject1 = New Class1
Set oObject2 = New Class1
Debug.Print oObject1.instanceName & " " & oObject2.instanceName

In the immediate window:

oObject1 oObject2

Is this possible in VBA?

If VBA runtime has a Symbol Table - since it is interpretive I think maybe it does - and I had a way of exposing it, then I could make a Property Get procedure to access the symbol Table and search on the Address - ObjPtr(Me) - to return the semantic name of the instance of the class.

I'm pretty sure this is a dumb question but, hopefully, the process of realising its a dumb question is helpful to my understanding.

Example of a Symbol Table

Address Type    Name
00000020    a   T_BIT
00000040    a   F_BIT
00000080    a   I_BIT
20000004    t   irqvec
20000008    t   fiqvec
2000000c    t   InitReset
20000018    T   _main
20000024    t   End
2 Answers

TypeName(obj) will return the Type of any variable in VBA:

Dim c as Class1
set c = new Class1 
Debug.print TypeName(c) '==> "Class1"

FYI, I've also historically wanted to access the symbol table also. The idea was to get local variables from the previous scope by name. In that way you could make string interpolation:

a = "World"
Debug.Print StringInterp("Hello ${a}")

https://github.com/sancarn/VBA-STD-Library/blob/master/docs/VBAMemoryAnalysis.txt https://github.com/sancarn/VBA-STD-Library/blob/master/docs/VBAMemoryAnalysis2.txt

No luck making a general function yet.

Related