sboggs11
sboggs11

Reputation: 199

C# How to create new object using classname stored in variable in constructor?

I have 8 forms (and counting) in a program. I have this repetitive code to instantiate OR bring back the "visible" of each form and would like to have a piece of modularized code that would perform these checks shown in this snippet:

//Some form classes:  
Form f2 = new Form_DisplayCustomersList();  
...and other forms too

...    

///The code that I would like to modularize, so I don't have to repeat it for every form:    
private void button1_Click(object sender, EventArgs e)    
{  
    //check form status and recreate. Show and activate - as needed.  
    if ((f2 == null) || f2.IsDisposed) 
    {  
        f2 = new Form_DisplayCustomersList();  
        showForm(f2);   
    } else 
    {  
        if (!f2.Visible) 
        {  
            showForm(f2);  
            f2.Activate();  
        } else 
        {  
            f2.Activate();  
        }  
    }           
}  

So I thought it would be nice to ALLOW VARIOUS button click events call a reusable method that would look something like this:

public void displayThatView(Form fx) 
{  
    if (fx == null || fx.IsDisposed) 
    {  
       // Form fy = new fx();  
        fx.Show;  
    }  
    else 
    {  
        if (!fx.Visible) 
        {  
            fx.Show;  
            fx.Activate();  
        }  
        else 
        {  
            fx.Activate();  
        }  
    }  
}

And to be able to call the method this way, where f2 could be any variable or the Form type:

private void button1_Click(object sender, EventArgs e)   
{
this.displayThatView(f2);
}

I am just a few months into C#. I am not sure if it is possible to do what I am trying, but it would eliminate some repetitive code!

Upvotes: 4

Views: 157

Answers (2)

Vlad
Vlad

Reputation: 1439

You can make this method as Extension:

public static class Extensions
{
    public static void displayThatView(this Form fx) 
    {  
        if (fx == null || fx.IsDisposed) {  
            Form fy = new fx();  
            fx.Show;  
        }  
        else {  
            if (!fx.Visible) {  
                fx.Show;  
                fx.Activate();  
            }  
            else {  
                fx.Activate();  
            }  
        }  
    }  
}

And call it:

private void button1_Click(object sender, EventArgs e)   
{
    this.displayThatView();
}

Upvotes: 0

dbc
dbc

Reputation: 116544

You can make a static generic helper method like so:

public static class FormHelper
{
    public static TForm ShowAndActivate<TForm>(TForm form) where TForm : Form, new()
    {
        if (form == null || form.IsDisposed)
        {
            form = new TForm();
            form.Show();
        }
        else
        {
            if (!form.Visible)
            {
                form.Show();
                form.Activate();
            }
            else
            {
                form.Activate();
            }
        }
        return form;
    }
}

The where TForm: Form constraint guarantees that your form variable is a (sub)class of Form. The where TForm : new() constraint guarantees that there is a parameterless constructor for this type of form. More here: http://msdn.microsoft.com/en-us/library/d5x73970.aspx.

By returning a TForm, you inform the caller if a new form was created.

Upvotes: 6

Related Questions