Link to home
Start Free TrialLog in
Avatar of siskinds
siskinds

asked on

Inheriting forms with Singleton

Hi All,
I have an MDI application where I am modifying data for a number of items that are all so similar I hope to use a single form and change the title and a couple of things on the form rather than create a bunch of forms that are all the same.  I'd also like to limit each instance of any given form type to 1.  I've tried using various forms of the singleton pattern, but it (by design) won't let me create multiple instance of the form so I tried to inherit from a base form to enforce singleton on the derived forms but was getting some weird errors that publically exposed method calls were not defined (?!)  Has anyone tried this type of thing?  I can do this with brute force looping on the MDI itself, but was hoping for something more "elegant"...
Thanks,
John

Avatar of Gautham Janardhan
Gautham Janardhan

is this what u are looking for

 class Temp
    {
        static ArrayList FObjectList;//for storing the objects
        static readonly int ObjectCount = 10;// max object count

        //static constructor
        static Temp()
        {
            FObjectList = new ArrayList();
        }
        //private constructor
        private Temp()
        {
        }
        //used instead of he contructor to create objects
        // like Temp Object = Temp.CreateObject();
        static Temp CreateObject()
        {
            if (FObjectList.Count >= ObjectCount)
            {
                return (Temp)FObjectList[10];
            }
            else
            {
                Temp FTemp = new Temp();
                return FTemp;
            }
        }

    }
Avatar of siskinds

ASKER

Hmmm, very close except this doesn't differentiate between the form types, only the number of forms allowed to be open at one time.  

Let's say I'm "managing" fruit.  I want to be able to have one and only one form for apples displayed at a time, one and only one for oranges, one and only one for bananas etc etc.  The particulars of the fruit are all the same, say, name, color and weight which is why I only want to use one form.  Somehow I need to keep track of the different fruit forms that are open...

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
looks good - will give 'er a try on Monday and report back.  Thanks gauthampi!
Okay back at it and have a few problems...

The compiler is warning me that I need to use the new keyword when declaring the derived classes CreateObject.

As mentioned, I am inheriting forms - I'm trying this code out on two seperate child forms (have another 6 that I want to do this with).  The first form inherited the controls fine, the second has not (at least at design time) then when running, the second form is bigger and the controls are all spaced differently to the base form or the first form I inherited from - weird.

When I open and close a form all is well, if I try to re-open the same form I crash in the MDI parent call to create and show the form.

Thanks!
Sorry, with the last problem I receive a ObjectDisposedException - I'm trying to access a disposed object named xyz...
can u post the code
I spent yesterday messing around with it and got something working okay, though it doesn't look like your suggestion anymore.  I can confirm that the odd sizing issue I had must have been VS as I ended up deleting the offending form and re-adding it with no further problems.  I ended up creating child forms via inheritance from the base form (as you suggested) and calling the child form from a menuitem in the MDI parent:
     Form f_apple = frmApple.CreateObject();
     f_apple.MdiParent = this;
     f_apple.Show();
     f_apple.BringToFront();

The child was created as such:
     private static frmApple f_apple = null;
     public frmApple()
     {
          InitializeComponent();
          this.Text = "Manage Apples";
          this.lblStakeholder.Text = "Apples";
     }

     public static frmApple CreateObject()
     {
          if (f_apple == null)
          {
               f_apple = new frmApple();
          }
          return f_apple;
     }

I also made sure I set f_apple = null in the Dispose method of the form.  This seems to ensure that there is only one form of type apple allowed at any one time.  I could never work out the idea you had with using the arraylist and objectcount variables, but would be very interested in finding out!  
Thanks, John.
j=k this a singleton model when u want to restricty to mutilple objects like 5 it wont work i think
Thanks again for your help on this gauthampi.  while your solution did not do exactly what i needed in this instance, I like the ideas you presented in regards to restricting forms to x number of instances.