On Fri, 11 Apr 2008 18:42:01 -0700, Saimvp
<Sa****@discussions.microsoft.comwrote:
Hello Marc Gravell and Machin.
Good Day.
Why your not using an Instance in the form and yet your using a New Form?
Im using it because I want to open a single form only. If Im using a New
Form1, In my command button If I will click the button to show the Form1
it
will show the Form1 equal the click in the button.
It seems to me that if you follow the general rule to always run your GUI
code on the original main GUI thread, Marc's concern about threading is
something you can safely ignore. I mean, it's a legitimate concern, but
it would only come up if you start trying to manipulate your user
interface from more than one thread. That's a good thing to avoid anyway.
Now, as far as other issues that might exist, Marc's pointed out one
other, specifically "multiple parenting". That is, if you want to provide
a parent for the form, then having just one form instance can lead to
problems if you wind up trying to show the form with more than one parent,
since all controls, including forms, can have only one parent. In your
sample code, you appear to be using the form as an MDI child form, which
may make this problem more of an issue. If you create a new instance each
time you want to show the form, this problem is avoided entirely.
Another issue has to do with resource management. Since you are showing
the form modelessly, when it's closed all, the form will be disposed.
This can be circumvented by changing all of the "close" logic in the form
so that the form is actually hidden, but doing that is a bit of a pain.
Again, creating a new instance each time you want to show the form avoids
the problem entirely.
It's not that there aren't reasons to design a form as a singleton. One
advantage is that the form can preserve its state very easily doing so,
without adding any extra code to save and restore the state for each new
instance. Another advantage is that if you are using the form in a way
such that it might already be visible and you want that existing form to
be used when the button is clicked, using a singleton is a convenient way
to do that.
But there are disadvantages, including those mentioned above. You should
consider whether the need for a singleton outweighs the disadvantages. In
most cases, it won't. In some cases, the best way to address your overall
design is to make the form a singleton.
It's not that there's a 100% rule. It's just that without knowing
anything else about your application, chances are that implementing the
form as a singleton is overkill and might needlessly complicate your
design in other areas.
What you suggestion to me. Im new in programming in C#.
Well, I think the suggestion Marc was making was to just create a new
instance of the form class each time you want to show it. Rather than
making the class follow the singleton pattern, don't bother with that and
just call "new frmNEmp()" to get a new instance each time you want it to
be shown.
Pete