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
Solved

Using a type variable

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

791 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