Get A Pointer In SubForm To Already Instantiated Class

dalski

Member
Local time
Today, 06:41
Joined
Jan 5, 2025
Messages
146
Class-A instantiates Class-B. I need a subform (outside of Class-A & Class-B) to gain access to the instance of Class-B. How do I get a pointer to the instance of Class-B? I could create an object variable of Class-B inside the subform but this would create a new instance of Class-B (undesired). I want to reference the existing Class-B object.

I have declared the Class-B object variable as public & would've thought I could gain access now but this has not worked.
 
Last edited:
Class-B object variable as public & would've thought I could gain access now but this has not worked
If your variable is public in a standard module then yes you should be able to reference directly

If your variable is public in a forms module then think of it as a property of the form. The subform needs to reference it through the form that instantiated it. Something like
Dim bb as clsB
set bb = forms("someform").SomeBvariable

If Class A instantiates an instance of Class B and holds the class variable then you have to go through Class A. Unless again it is intantiating a public variable in a standard module. Which seems like a bad design.
 
Thanks @MajP, understood.
Which seems like a bad design.
That's worrying, hopefully this will change your opinion. Class-A & Class-B's methods & properties are accessed elsewhere. They decouple the current subforms & allow these methods... to be used for other purposes elsewhere in different instantiations of different classes.
I could put all these inside the subform, but then I would need to create another class/ module to contain these identical methods. I thought I was being good by decoupling these methods to be used elsewhere. I agree that it's not nice having the subform in title being tightly coupled with this particular instance of Class-A but i can't see a way around this. I'll be sure to terminate the circular reference. They are in essence tightly coupled & Class-A drives Class-B & the subform. The methods have been decoupled, BUT Class-B allows me to use these beautifully encapsulated methods... elsewhere in many different areas. Possibly a beautiful design :ROFLMAO:?
 
Last edited:
It's not crystal clear to me if you want to have only 1 instance ever of ClassA and ClassB.
If so, then you can use a Static class, like clsErrorHandler in Northwind 2 Developer Edition template.
 
Thanks Tom, there will be multiple instances of Class-A & B later, was unaware of static class so thanks for future.

I failed to mention that Class-A was in a different subform (call it subform-A) when it instantiated the public Class-B object variable. So it seems that because subform-A has lost focus it no longer contains the publicly module level which I was completely unaware of & failed to mention this essential info in the OP (sorry).
I think I'm best to create an instance of Class-B in subform-X to gain access to use it's methods...
 
Thanks Tom, there will be multiple instances of Class-A & B later, was unaware of static class so thanks for future.

I failed to mention that Class-A was in a different subform (call it subform-A) when it instantiated the public Class-B object variable. So it seems that because subform-A has lost focus it no longer contains the publicly module level which I was completely unaware of & failed to mention this essential info in the OP (sorry).
I think I'm best to create an instance of Class-B in subform-X to gain access to use it's methods...

Are both the sub-form-A with instance of Class-B and sub-form-B inside the same main form?

If yes, add a public property to the main form that returns the reference to Class-B in sub-form-A. Then in sub-form-B use that property.

sub-form-B

Set InstanceVar = me.Parent.Form.TheProperty
 
Are both the sub-form-A with instance of Class-B and sub-form-B inside the same main form?

If yes, add a public property to the main form that returns the reference to Class-B in sub-form-A. Then in sub-form-B use that property.

sub-form-B

Set InstanceVar = me.Parent.Form.TheProperty
Would you agree that with the several explanations and amendments, it's about time for a very simple repro database? Might take you less time and would be much more fun for us.
 
was unaware of static class so thanks for future
To be clear there is no such thing as a static class in VBA. When they say that clsErrorHandler in Northwind is static class that is incorrect and misleading. Personally I think they should change the verbiage to a "Pseudo Static Class".
In Northwind 2 it states
This is a static class, meaning the developer does not need to instantiate it, it is always there.
' To use it in your own projects you have to use this procedure: Export File out of this database, and Import File into your database.
' A simple copy/paste of the code is NOT enough.
That is incorrect. It is a predeclared instance of the class. IMO they should explain what is going on and how to create a predeclared class. They provide a link but it is only a definition of the attribute not how to do it. If the purpose of Northwind is to teach they dropped the ball here.

See here for a better discussion of how to make a predeclared class.
.
Chatty explains the difference better between a static class and a predeclared class in vba.
A predeclared class in VBA and a static class (as found in languages like C#) are not the same concept, although they share some functional similarities in how they can be accessed.

Predeclared Class in VBA:
  • In VBA, a class module can be set with the VB_PredeclaredID attribute set to True. This creates a default instance of the class that is automatically available and can be accessed directly using the class name, without needing to explicitly declare and create an object variable using New.
  • This behavior is similar to how Form modules work in VBA, where you can refer to Form1.TextBox1.Text directly without explicitly creating an instance of Form1.
  • While it provides a readily available instance, it's still an instance of the class, meaning it can hold state and you can create additional instances of the same class if needed.
Static Class (in other languages like C#):
  • A static class, in languages that support it, is a class that cannot be instantiated. You cannot create objects of a static class using the new operator.
  • All members (methods, properties, fields) within a static class must also be static.
  • Static classes are typically used for utility functions or collections of related methods that do not require an object instance to operate and often do not maintain state specific to an instance.
Key Differences:
  • Instantiation:
    A predeclared class in VBA can be instantiated (you can create new instances), even though a default one is provided. A static class cannot be instantiated.
  • State:
    A predeclared class's default instance can hold state specific to that instance, and other instances can have their own state. A static class, by design, typically does not hold instance-specific state.
  • Purpose:
    Predeclared classes in VBA are primarily a convenience for accessing a single, default instance easily. Static classes are a design pattern for groups of related functionality that don't require object instantiation.
In summary, while both allow direct access without explicit object creation, a predeclared class in VBA provides a readily available instance of a class, whereas a static class represents a type that cannot be instantiated and contains only static members.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom