Solved

Using a type variable

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert image with its metadata into SQL server Database 3 87
Allow space in this pattern 2 46
Angular JS Route 3 45
Reset asp.net login password 4 25
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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

18 Experts available now in Live!

Get 1:1 Help Now