Single instance of a class to be available to all instances or all other classes

I am developing some class modules to run a specialised simulation and need a simulation clock to be available to the objects in the simulation.

I have a clock class that returns the time as a property, see below.

How can I make the one clock available to all other objects?  

Private piTime As Integer

Private Sub Class_Initialize()

    piTime = 0

End Sub

Public Property Get Time() As Integer

    Time = piTime

End Property

Public Sub Tick()

    piTime = piTime + 1

End Sub

Public Sub Clear()

    piTime = 0

End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rory ArchibaldCommented:
You would need all your other classes to have a property (say 'Clock') to which you assign the clock instance when you create each instance of the other classes.
sjgreyAuthor Commented:
I have a Class_Initialize subroutine within the modules that use the clock but I don't see, to be able to pass it parameters.  Will I have to use a separate "Let" procedure with a separate line such as

Object.Clock = SimClock

followed by, within the object

Public Property Let Clock(SimClock as clsSimClock)
Set Clock = SimClock
End Property

to establish the connection to the clock or is there an easier way to include it when instantiating the modules that need the clock?
Rory ArchibaldCommented:
VBA doesn't give you a constructor you can use, so you'll need the property though it needs to be a Property Set rather than Let as you are passing an object.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sjgreyAuthor Commented:
That worked thanks.  Code in the class required to use the clock as follows in case it is of interest to anyone else.

Private pClock As clsSimClock

Public Property Set SimClock(Clock As clsSimClock)

    Set pClock = Clock

End Property

Open in new window

And in the code that sets up the class instances

    Dim SimClock As clsSimClock
            Set Unit.SimClock = SimClock

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.