Sharing properties across classes

M

Morten Snedker

How do I share a property across classes?

For example I'll have three classes:

- Settings
- Class1
- Class2

Class1 will initiate the class Settings with a New Settings and will
followingly set the properties in the class Settings.

After Class1 has set the properties of Settings, Class2 will use these
already set properties, but without a New.

How do I access the property values set by Class1 - from Class2? I
should I consider another approach?

Regards /Morten Snedker
 
G

Göran Andersson

Morten said:
How do I share a property across classes?

For example I'll have three classes:

- Settings
- Class1
- Class2

Class1 will initiate the class Settings with a New Settings and will
followingly set the properties in the class Settings.

After Class1 has set the properties of Settings, Class2 will use these
already set properties, but without a New.

How do I access the property values set by Class1 - from Class2? I
should I consider another approach?

Regards /Morten Snedker

Take a look at the singleton pattern. By making the Settings class a
singleton, the Class1 class would get the instance of the Settings class
(and thereby creating it as it's the first use of the class). Then the
Class2 class could get the instance of the Settings class, and this
would be the same instance as the Class1 class initiated.
 
M

Michel Posseth [MCP]

You could also just use a module ,,, :-|

or just pass the properties to the other classes as parameters in the
constructor

just 2 extra simple solutions for your "problem"

HTH

Michel Posseth
 

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