Use ObjPtr (Me) to return a custom class instance name?

I understand that ObjPtr will return the address of the object in memory and that it points to a structure called IUNKNOWN and that there is some kind of interface definition encoded in such a way as to show the structure of the object, but I could not figure out how to define the interfaces for VBA Custom Class Object and how to use this to return the Name property of an object.

This is more โ€œnice to haveโ€ than necessary, but I just want to know the name of the object instance at run time to include it in the trace messages.

Can someone explain how to do this or, better yet, direct me to the link so that I can understand this?

EDIT

To re-indicate my goal:

To create custom class objects that can determine the name of their specific instance.

for instance

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

In the nearest window:

 oObject1 oObject2 

Is this possible in VBA?

If the VBA runtime has a Character Table - as it is being interpreted, I think maybe it is, and I had a way to expose it, then I could create a Property Get procedure to access the Table character and search by address - ObjPtr (Me) - to return the semantic name of the class instance.

I am sure this is a stupid question, but I hope that the process of recognizing your dumb question is useful for my understanding.

Character table example

 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 
+4
source share
1 answer

Take NO for an answer. Cannot return instance name as string literal in VBA.

I still donโ€™t understand the reason why you can do this ... Anyway

The easiest way to find out the name of each instance is to create a property for the class in which the actual name is stored. This will result in the name being only as a String property, and not the actual reference to the object - it already has a link - itself!

So create a class module

Class1

 Option Explicit Public MyName as String 

and in Module1 all that is required 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 is to create a dictionary to store 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 you can do what you want, but that would be very ugly. This is how I am not going to demonstrate.

You would create an instance of a custom class. Using ObjPtr, you can get this link in memory. Then you need a mechanism that scans your module code line by line and finds the names of all the variables that you defined. After you get a list of all the variables, you will need a mechanism that tries to create an instance of the same type (class). Once you get past this point, you can try myNewObj = c ("c" is an obj instance) programmatically. If this succeeds, you must do ObjPt for both and match your addresses in memory - you will get a match that you know, the name of the variable. Grree, please do not do it like this: P

+1
source

Source: https://habr.com/ru/post/970718/


All Articles