Solved

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

Posted on 2012-03-21
4
208 Views
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

0
Comment
Question by:sjgrey
  • 2
  • 2
4 Comments
 
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.
0
 
LVL 1

Author Comment

by:sjgrey
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?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 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.
0
 
LVL 1

Author Closing Comment

by:sjgrey
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

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now