tags:

views:

53

answers:

2

hi, i want declare on form as global variable and then work with that in app. i dont want use below method to access app forms.

Form1 myForm = new Form1();

anyone have any idea?

thanks

A: 

my way is declaring a static class which have static variable Form1.

public static class Global
{
    private static Form1 myForm = new Form1();
    public static bool Show()
    {

        myForm.Show();
    }

    public static bool Hide()
    {
        myForm.Close();
    }
}

it's not good way?

Sadegh
It's not. This code will bomb when the user closes the form. If you prevent the user from closing the form it is a permanent memory leak. Why do you need this?
Hans Passant
you'll have to make sure Close() is never called, because that disposes the form.
Jason Diller
nobugz: i need this because i want show/hide Form1 eachtime i want.Jason: thanks, i changed close to hide.
Sadegh
Make sure you also cancel any user-invoked close on the form and hide it instead, or you'll have the same problem
Jason Diller
+3  A: 

Looks like you want your form to be a Singleton, which isn't my favourite pattern, but it beats a global.

Jason Diller
A singleton will do as long as you only Hide and do not Close the Form. But indeed, better than a global.
Henk Holterman
Henk is correct. Calling close will cause the form to be disposed, which means you'll have to recreate it in order to show it again, which would defeat the purpose of the singleton. I agree with the other posters that are suggesting the whole idea might be a bad one, but if you have a good reason, it can be done.
Jason Diller