CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    design issue with static vars

    I have a design issue while using 2 static vars, maybe someone can advise.
    I have 2 static variables, both containing a list of data read in from 2 seperate xml files.
    I have made the vars static because I would like to be able to access them without an instance and wish 2 only have a single instance of each no matter how many times the outer object is created.
    I also have 2 objects which have a relationship with each of these static objects. One object uses one of the static objects and the other object uses the other static object.
    the scenario is 2 xml files, one is entered by the user and the other is always the same file.
    I have a manager that loads the 2 files and reads contents of each file into an object which contains a list.
    I have presently implemented this in such a was that the manager instantiates 2 new objects, each of these objects has 2 member static variables. So when one object is newed for the first file, the 2 static vars are initialized but on the first is added to, when the second object of the same type is intialized the second static varialble, which has already been initialised is added to.
    This is hard to explain, but it seems over complicated to me.

    Here's the type of structure


    Here's a snap of some of the code:


    Code:
    ..................
    
    public class BaseClass
        {
            .........
            protected  ClassWithStatic _classWithStatic;
            
            public BaseClass(string name, string[] parameters)
                
            {
                _classwithstatic = new ClassWithStatic();//this objects contains the 2 static objects
                _classwithstatic.AddToFirstStatic(s);//This adds to the 
    
    --------------------------------------------------------------------------------------
    
    public class DerivedClass : BaseClass
        {
            protected OtherObject _contents;
    
            public DerivedClass(string name, string[] parameters, string[] contents)
                : base(name, parameters)
            {
                _contents = new OtherObject(contents);
                _classwithstatic.AddToSecondStatic(s);//This adds to the 
    ..............
                
            }
                    
        }
    
    
    ----------------------------------------------------------------------------------------
    
    
    
    public class ClassWithStatic
        {
            private static System.Xml.XmlDocument _firstStatic;//The static object
            private static System.Xml.XmlDocument _secondStatic;//the static object
    
            public ClassWithStatic()
            {
                if (_xmlDoc == null)
                {
                    Init();
                }
            }
            private void Init()
            {
                _firstStatic = new System.Xml.XmlDocument();
                _secondStatic = new System.Xml.XmlDocument();
                   
           .....................
     //Also 2 getters
           public static System.Xml.XmlDocument GetFirstStatic()
            {
                return _firstStatic;
            }
    
            public static System.Xml.XmlDocument GetSecondStatic()
            {
                return _secondStatic;
            }
    Last edited by ireland; July 12th, 2006 at 05:00 AM.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: design issue with static vars

    I'm not sure if I understood you well, but it seems to me that you are surprised that after creating the second instance of ClassWithStatic, you lost the loaded data. It is because you write this code this way - you initialize your statics in instance constructor, which is called everytime an instance is created.

    One solution is to initialize ClassWithStatic in "class" constructor
    Code:
    static ClassWithStatic()
            {
                if (_xmlDoc == null)
                {
                    Init();
                }
            }
    But dealing with static variables is more complicated than neccessary. Look at singleton desing pattern at http://www.dofactory.com/Patterns/PatternSingleton.aspx
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Re: design issue with static vars

    The Singleton pattern maybe the answer.
    Your contructor example earlier has the cnst as static, this would make the outer object static as well as it's 2 member variables.

    I shouldn't lose any data , if the member objects are static then no matter how many times new is called on the outer object the member vars same instance should still remain! isn't that correct?

    I initialize the statics in the Init() func if the static is null, but the real population of the static is only done later. The real popultation is done later with 2 seperate function calls, one for each static.
    so the outer object instance is created and 2 static instances of the static objects are created. The xml files are later read in and populate the statics with the Xml file data. The data is not lost but there are 2 problems, the constructor is called each time even though the Init() is only called if the instance of _xmldoc is null, also

  4. #4
    Join Date
    Jan 2001
    Posts
    22

    Re: design issue with static vars

    What is _xmlDoc?


    are you referring to one of the static xml documents?

  5. #5
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Re: design issue with static vars

    yeh it's a static XmlDocument.

  6. #6
    Join Date
    Jan 2001
    Posts
    22

    Re: design issue with static vars

    Well, I don't see _xmlDoc defined anywhere is why I ask.

    When I did a quick example, the first new runs the Init function,
    and the Init function never gets called again.
    However, I check versus _firstStatic, not _xmlDoc.

    Unless you somehow leave the xml document you're checking against as a null, I don't understand the problem.

    The way you have it set up should work.

    You may want to split up your null checks into Init_first and Init_second, just to be safe(r).

  7. #7
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Re: design issue with static vars

    you're right of course, _xmlDoc is in fact _firstStatic, that was just a typo.
    I know it works but it looks over complicated to me. Anyway thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured