Class (Static) Methods in VBA

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Class (Static) Methods in VBA

Class (Static) Methods in VBA: Easy Solutions to a Common Problem 💪

Are you struggling with creating class methods in VBA? You know, those methods that can be called without having an object of the class? Well, buckle up, because we're about to dive into the world of static methods in VBA! 🚀

In languages like C++ and Java, you can simply use the 'static' keyword to create these handy class methods. But in VBA, it's not as straightforward. However, fear not! We have the solution you've been looking for. 😉

Let's take a look at an 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 Problem

As you can see, we have a Person class with a Create method that we want to make static. When we try to call the Create method without instantiating an object of the Person class, we encounter an error.

💡 The Solution

To make your VBA class method static and accessible without an object, you'll need to employ a clever workaround using a regular module. Here's how you do it:

  1. In your VBA project, insert a new module by clicking on Insert > Module.

  2. Inside the new module, declare a function with the same name as your class method and specify the return type as the class type you want to create. In this case, it's the Person class.

    Public Function Create(name As String) As Person
  3. Next, create an instance of the class and set it to the function name. Then, you can call the instance's original method.

    Set Create = New Person Create.name = name
  4. Finally, you can now use your static method by calling it like any other regular function.

    Set p2 = Create("Bob") ' Works like magic! 🎩✨

And that's it! By utilizing this workaround, you can create class methods in VBA that behave like static methods. 🙌

📣 Reader Engagement: Share Your Thoughts!

Now that you're armed with the knowledge of creating static class methods in VBA, why not put your skills to the test? Try implementing this workaround in your own project and see the results for yourself.

We'd love to hear your thoughts and experiences. Let us know in the comments below how this solution worked out for you. If you have any questions or alternative approaches, feel free to share them too! Let's learn and grow together. 💡🌱

Happy coding! 👩‍💻👨‍💻

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my