CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  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.

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