Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using a type variable

Posted on 2004-09-23
3
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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