I wonder, whether it is possible to create class-methods in VBA. By class-method I mean methods that can be called without having an object of the class. The 'static'-keyword does that trick in C++ and Java.
In the example below, I try to create a static factory method.
Example:
'Classmodule Person' Option Explicit Private m_name As String Public Property Let name(name As String) m_name = name End Property Public Function sayHello() As String Debug.Print "Hi, I am " & m_name & "!" End Function '---How to make the following method static?---' Public Function Create(name As String) As Person Dim p As New Person p.m_name = name Set Create = p End Function 'Using Person' Dim p As New Person p.name = "Bob" p.sayHello 'Works as expected' Set p2 = Person.Create("Bob") 'Yields an error'
The Static statement is used to declare specific variables within nonstatic procedures to preserve their value for as long as the program is running.
What is a static method? Static methods, much like class methods, are methods that are bound to a class rather than its object. They do not require a class instance creation. So, they are not dependent on the state of the object.
A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance).
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
1. Create a normal class containing the public method(s) you need to be 'static'
2. Include a public method [in this 'static' class] that initialises the [private] 'static fields' within the class (it can take parameters if you wish)
3. Create a module acts as a factory
Public Function CreateStaticClass(parameters for 'constructor') As StaticClass Dim static As StaticClass Set static = New StaticClass Call StaticClass.Constructor(pass in parameters) Set CreateStaticClass = static End Function
4. you can now use the 'static' class by calling CreateStaticClass('parameters').MethodName('parameters') there is no need to initialise the instance as that is done by the factory method
5. (Optional) If you want to enforce singleton instances you can create a module that acts as a singleton container - include a private instance variable and a public accessor property. optionally you can use a 'let' setter to allow the singleton to be 'replaced' with a new [static] class (using different constructor parameters - see #2,3). Use 'Let' for the setter, so you can assign the singleton without using 'set' ala OO languages
Private curStaticClass as StaticClass Public Property Get CurrentStaticClass() As StaticClass If curStaticClass Is Nothing Then Set curStaticClass = CreateStaticClass Set CurrentStaticClass = curStaticClass End Property Public Property Let CurrentStaticClass(value As StaticClass) If Not (curStaticClass Is Nothing) Then Set curStaticClass = Nothing Set curStaticClass = value End Property
6. To assign the singleton:
CurrentStaticClass = CreateStaticClass(parameters)
7. To use the singleton:
[value = ] CurrentStaticClass.MethodName(parameters)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With