Solved

Using a type variable

Posted on 2004-09-23
3
180 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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