Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2012-03-21
Medium Priority
Last Modified: 2012-03-22
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

Question by:sjgrey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 85

Expert Comment

by:Rory Archibald
ID: 37751603
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.

Author Comment

ID: 37751912
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?
LVL 85

Accepted Solution

Rory Archibald earned 1500 total points
ID: 37751990
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.

Author Closing Comment

ID: 37755664
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


Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question