Vincent Dagpin
Vincent Dagpin

Reputation: 3611

Type Casting in c#

I have a problem with declaring a type as passed from parameter type variable.

public static void ShowThisForm(Type passXtraForm, Form parent)
    {
        var xfrm = passXtraForm.GetType();
        xfrm xfrmName = new xfrm();
        xfrmName.Show();
    }

Can I declare a variable as a type from passXtraForm.GetType() and declare it to another variable? Just passing the type of form to another class.

Thanks in advance for the response.

Upvotes: 1

Views: 198

Answers (5)

jmoreno
jmoreno

Reputation: 13561

You can instantiate a class/type via reflection and Activator.CreateInstance(typ, constructorargs), but most likely it would be better to create the object elsewhere and have it be of either a known base class (in your case it looks like form), or have it implement a defined interface that can be used to manipulate it. Very rarely will you need to create an object of totally unknown type.

Upvotes: 1

Michael Paulukonis
Michael Paulukonis

Reputation: 9100

Maybe pass in a factory, so that a known interface can instantiate what ever type of object the calling code deems is necessary?

(the below code is a quick sample I typed in here; formatting is off, and design could be better. It's just to give an idea)

static class XFactory
{

  private int _id;

   public XFactory(int formId) {
      _id = formId;
   }

   /// <summary>
   /// Decides which class to instantiate.
   /// </summary>
   public static Form Get()
   {
     switch (_id)
     {
    case 0:
        return new FormA();
    case 1:
    case 2:
        return new FormB();
    case 3:
    default:
        return new FromC();
     }
   }
}


public static void Main()
{

  ShowThisForm(new XFactory(2));

}

public static void ShowThisForm(XFactory formFactory)
    {
        var xfrm = formFactory.Get();
        xfrm.Show();
    }

Upvotes: 1

mtijn
mtijn

Reputation: 3678

you could use generics for this:

public static void ShowThisForm<T>(T passXtraForm, Form parent) where T : Form, new()
{
     T xfrmName = new T();
     xfrmName.Show();
}

in this case the type argument is restricted to the Form type and types deriving from Form. anyway, why are you having this method? there are other methods in the Form and Application static classes for finding the forms out there in your app...

Upvotes: 4

Donut
Donut

Reputation: 112815

First of all, it doesn't look like you need the parent parameter; I'd eliminate it entirely. Then I'd use generics to do what you're trying to accomplish:

public static void ShowThisForm<T>() where T : Form, new()
{
    T xfrmName = new T();
    xfrmName.Show();
} 

The where T : Form, new() portion of this code is called a type constraint and it prevents you from calling the ShowThisForm method with a type that doesn't have a default constructor and derive from Form.

By indicating that T must have a default constructor, the compiler knows how to resolve new T(); by indicating that T derives from Form, the compiler knows how to call the .Show() method.

So, if you have a form class called MyForm, you could use the following syntax to call this method:

ShowThisForm<MyForm>();

For more documentation, you should take a look at these articles on MSDN:

Upvotes: 3

Marc
Marc

Reputation: 9354

You could change it to something more like this:

    public static void ShowThisForm<T>() where T : Form, new()
    {
        T xfrmName = new T();
        xfrmName.Show();
    }

Upvotes: 1

Related Questions