locked
OOP using shared property to get global like functionality RRS feed

  • Question

  • User-881450349 posted

     I'm not sure what forum I should have posed this question too, but hopefully this is a good start:

    I have a class and I need to create multiple instances of this class. Each of the properties must be accessible from anywhere in the project. So I decided I should make the properties shared. Problem with this then is that these properties change in unison for all instances of the class.

    So, I figured I would not even make instance of the class. Rather I would make new classes (I only need a few), and have them inherit the base class. Same problem though, the properties still change in unison as they were set to shared in the base class and thus will always be shared it seems.

    Can anyone think of a clever way around this problem? Doesnt matter to me whether I need to create instances of the classes or multiple classes, I simply need to have multiple copies of the properties in this base class and have them accesible on a global level (without creating a whole lot of redundant code).

    Any thoughts?

    Thanks,

    Sam

    Sunday, September 5, 2010 4:35 PM

Answers

  • User-2004844803 posted

    hi,

    you say you need multiple instances off your class but the class is not bound to a specific context. I suppose you have some  kind off id or key that you could use when you need to access a specific instance? If you do I would create a singleton that acts as a single entry point (a manager) and allow this manager to hold a list off all instances. Expose a get method to access to correct instance.  It should look something like this:

    Manager.Instance.Locate("some key to a specific instance");


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 7, 2010 6:47 AM

All replies

  • User1945922531 posted

    It smells like a singleton pattern, to me ;)

    http://en.wikipedia.org/wiki/Singleton_pattern

    Monday, September 6, 2010 4:19 AM
  • User-861155893 posted

    Okay here's my two cents:

    Shared (or static in c#) methods or properties = single instance (as you have noted)

    To get mutliple copies of the propeties for an object but have them accessible globally just have them as public...

    i.e.

    class MyClass{
       public MyClass(){}
       public string MyProperty { get; set; }
    }

    Then you can do

    MyClass class1 = new MyClass();

    class1.MyProperty = "something";

    MyClass class2 = new MyClass();

    class2.MyProperty = "something else";

    as far as it being 'global' the property is visible whenever you have scope of the new ly created object instance.

    With regards to the 'Smells like a singleton' comment from the poster ignore it. the singleton pattern as it's name infers is to have a single class instance where you need multiple copies of the instance members.

    Monday, September 6, 2010 4:41 AM
  • User-881450349 posted

    Damien, is this what you meant in your point number one? :
    Public Class MainClass
        Public Shared TestClassInstance1 As New TestClass
        Public Shared TestClassInstance2 As New TestClass
    End Class
    I create a main class and create public shared instances of my other classes such that the class instances are independent yet they (and there public properties, methods) are accessiblie anywhere via:
    MainClass.TestClassInstance1.SomeProperty

    Problem is that I then need to create an instance of the class somewhere, and I don't have anywhere to put it such that everything that needs it also has scope of it.
    So building on this, if I make the class instances shared, then I don't need to create an instance of MainClass to access the class instances.

    Public Class MainClass
        Public Shared TestClassInstance1 As New TestClass
        Public Shared TestClassInstance2 As New TestClass
    End Class

    The TestClass instances are available anywhere via:
    MainClass.TestClassInstance1.SomeProperty

    This might work for me but it seems a bit sloppy. Anyone have any comments, other suggestions?

    Thanks,
    Sam

    Monday, September 6, 2010 10:51 AM
  • User-257361011 posted

    Hi,


    I think you can use list var and store all the objects of the class in that by doing this you can get all property values.


    Monday, September 6, 2010 12:38 PM
  • User-2004844803 posted

    hi,

    you say you need multiple instances off your class but the class is not bound to a specific context. I suppose you have some  kind off id or key that you could use when you need to access a specific instance? If you do I would create a singleton that acts as a single entry point (a manager) and allow this manager to hold a list off all instances. Expose a get method to access to correct instance.  It should look something like this:

    Manager.Instance.Locate("some key to a specific instance");


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 7, 2010 6:47 AM