Sharing properties across classes

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
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
 
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.
 
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
 
Back
Top