Solved

Using a type variable

Posted on 2004-09-23
3
178 Views
Last Modified: 2012-05-05
I would like to create a generic method that uses a type (or class) as a variable. In other words, can you tell me why the following code does not work and what I should do to fix it:

  System.Type typeForm = frmCourses.getType();
  Form frmNew = new typeForm();
0
Comment
Question by:pgormley
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 250 total points
ID: 12140101
You can create an object from a type like this:

    System.Object obj = System.Activator.CreateInstance(typeForm);
    Form bf = (Form)obj;

This assumes, of course, that obj was indeed a Form.

There are a couple of ways to retrieve a type.  You can get it from a live instance somewhere or call Type.GetType(<type_name>).  You can also get a type from the assembly where it lives.  Then you can create an(other) instance from the type.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12140115
If you want a runtime-discovered plug-in type implementation and you want to have a base form class other than Form, you need to implement the base class in a library that you can reference in the plug-in projects and in the main project where you are going to do the object creation and cast.  If you just need to have a bunch of Form classes and they will al be referenced in the main project then you don't care about this.
0
 
LVL 3

Expert Comment

by:chintan_vaishya
ID: 12140785
Hi,

YOu can use the Activator.CreateInstance(System.Type) to solve this problem

object theObj = Activator.CreateInstance(typeof(frmCourses));
frmCourses theForm = (frmCourses)theObj;

What activator.Createinstance does here is, it will search for Zeroparameter Constructor for that type of Form, and will create object from that constructor. If you want to pass other parameters, you can pass via object[] after the system.Type parameter in the createinstance() method.

Hope this helps you.
Good luck.
Chintan.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
Converting Newtonsoft.Json.Linq.JToken to a DateTime 4 73
asp.net mvc5 3 38
VB.NET HttpWebRequest 12 34
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now