"Static" Methods in Class Modules

R

Ripan

Is there anyway to create static methods in VBA Class Modules. By this I
mean methods that can be accessed from outside the Class Module without
making an instance of the object.

As a more concrete example:

I have a class module to hold a Time (a value for hour, minute, and
AM/PM). I declare various variables that are instances of the Time
object. I want to see if one time is before another but I do not
necessarily want to have to create an instance of a Time Object to do
that. (I want to be able to call functions by using Time.x as you can
do in regular Methods).

I know this is possible in the C# and C++ frameworks through the use of
Static Methods. Is it possible in VBA?

Thanks for any help.
 
S

solex

Ripan,

I am new to excel programming but have been programming VB for quite some
time, with that said I would assume that it is not possible to create a
static class method in VBA since the Global Multi Use (GMU) option is not
available for the instancing property of a class.

You could how ever create the method in a module and simply use
module.method syntax as you suggested below.

Dan
 
P

Patrick Molloy

To "use" a class module you'd need to instantiate it.
You can use variables and declare them as Static. Also
you can define your own Type
However, as an alternative, to could save your "times"
into a scripting.dictionary - set a reference to
scripting runtime). It looks and feels like a collection
but it also has a .Exists() method that returns a boolean
if the key exists or not.

Patrick Molloy
Microsoft Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top