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

excel, vba

Solution

Take `NO` for an answer. It's not possible to return an instance name as a String literal in VBA.

I still don't understand the reason you may want to do that... Anyway

The easiest way to know each instance code name would be to create a property for a class that stores the actual name. This would only expose the name as a String property and not an actual reference to the object - it already has a reference - itself!

So create a class module

Class1

Option Explicit

Public MyName as String

and in Module1 all it takes is

Option Explicit

Sub Main()

    Dim c As Class1
    Set c = New Class1

    c.MyName = "c"

    Debug.Print c.MyName

End Sub

And there you go :)

Another way would be to create a Dictionary to store both KEY/VALUE pairs.

Sub Main()

    Dim c As Class1
    Set c = New Class1

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "c", c

    Debug.Print dict.Exists("c")

End Sub

Now, it's possible to actually do what you want but it would be a really ugly way. Here's how as I am not going to demonstrate.

You would create an instance of a custom class. Using ObjPtr you can get it's reference in memory. Then you would need a mechanism that scans your module code line by line and finds names of all variables you've dimensioned. Once you retrieve a list of all variables you would need a mechanism which tries to create an instance of the same type (class). Once you get past that point you could try to `myNewObj = c` ("c" would be the obj instance) programmatically. If that succeed then you would do ObjPt for both and match their addresses in memory - you get a match you know the variable name. Grree please do not do it that way :P

Problem

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 ```

Original source