473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

create an instance with a string Name

hi.
If I have a form, like FrmAbout, can I create a form instance only with a
string "FrmAbout"?
not like : Form frm = new FrmAbout();
I want the code : Form frm = createInstance( "FrmAbout" );
How to code my createInstance function?

Thanks.
Mar 4 '06 #1
6 5553
There are several ways. You can use Activator.CreateInstance() or
Type.InvokeMember. I don't remember the pros and cons of these methods but
you can probably find out which is most appropriate for your application.

http://msdn.microsoft.com/library/de...tancetopic.asp

http://msdn.microsoft.com/library/de...embertopic.asp
"wu jianhua" wrote:
hi.
If I have a form, like FrmAbout, can I create a form instance only with a
string "FrmAbout"?
not like : Form frm = new FrmAbout();
I want the code : Form frm = createInstance( "FrmAbout" );
How to code my createInstance function?

Thanks.

Mar 4 '06 #2
Hello wu,

See this code. Comments expain how it works

// Get current assembly
Assembly frmMain = Assembly.GetEntryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType("WindowsApplication1.frmAbout");
// Create instance of form
object pc = Activator.CreateInstance(formType);
// Cast to Form class
Form frm = (Form)pc;
// Show Form
frm.Show();

wj> hi.
wj> If I have a form, like FrmAbout, can I create a form instance only
wj> with a
wj> string "FrmAbout"?
wj> not like : Form frm = new FrmAbout();
wj> I want the code : Form frm = createInstance( "FrmAbout" );
wj> How to code my createInstance function?
wj> Thanks.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 4 '06 #3
Hello Michael ,

And instead of using Assembly u can change these 2 lines below

// Get current assembly
Assembly frmMain = Assembly.GetEntryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType("WindowsApplication1.frmAbout");

on this

Type formType = typeof(WindowsApplication1.frmAbout);

MN> Hello wu,
MN>
MN> See this code. Comments expain how it works
MN>
MN> // Get current assembly
MN> Assembly frmMain = Assembly.GetEntryAssembly();
MN> // Get Type of your form
MN> System.Type formType =
MN> frmMain.GetType("WindowsApplication1.frmAbout");
MN> // Create instance of form
MN> object pc = Activator.CreateInstance(formType);
MN> // Cast to Form class
MN> Form frm = (Form)pc;
MN> // Show Form
MN> frm.Show();
wj>> hi.
wj>> If I have a form, like FrmAbout, can I create a form instance only
wj>> with a
wj>> string "FrmAbout"?
wj>> not like : Form frm = new FrmAbout();
wj>> I want the code : Form frm = createInstance( "FrmAbout" );
wj>> How to code my createInstance function?
wj>> Thanks.
MN> ---
MN> WBR,
MN> Michael Nemtsev :: blog: http://spaces.msn.com/laflour
MN> "At times one remains faithful to a cause only because its opponents
MN> do not cease to be insipid." (c) Friedrich Nietzsche
MN>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 4 '06 #4
Hello rmacias,

The IDasm code is similar, except that InvokeMemeber just call virtual function
InvokeMember and CreateInstance is direct call
Using CreateInstance is more readable
r> There are several ways. You can use Activator.CreateInstance() or
r> Type.InvokeMember. I don't remember the pros and cons of these
r> methods but you can probably find out which is most appropriate for
r> your application.
r>
r> http://msdn.microsoft.com/library/de...ary/en-us/cpre
r> f/html/frlrfsystemactivatorclasscreateinstancetopic.asp
r>
r> http://msdn.microsoft.com/library/de...ary/en-us/cpre
r> f/html/frlrfsystemtypeclassinvokemembertopic.asp
r>
r> "wu jianhua" wrote:
r>
hi.
If I have a form, like FrmAbout, can I create a form instance only
with a
string "FrmAbout"?
not like : Form frm = new FrmAbout();
I want the code : Form frm = createInstance( "FrmAbout" );
How to code my createInstance function?
Thanks.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 4 '06 #5
it is so great.
Thanks to all.
Mar 4 '06 #6
"wu jianhua" <bi*****@hotmail.com> a écrit dans le message de news:
eP**************@TK2MSFTNGP09.phx.gbl...
| If I have a form, like FrmAbout, can I create a form instance only with a
| string "FrmAbout"?
| not like : Form frm = new FrmAbout();
| I want the code : Form frm = createInstance( "FrmAbout" );
| How to code my createInstance function?

Take a look at Activator.CreateInstance(...)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
0
by: Dhananjayan | last post by:
Hi, I have a webservice(eg.IStringServicePort) running on Axis, iam able to create a java client for the same and invoke the service and obtain the result. I wanted to create a .Net client(C#)...
5
by: PJ | last post by:
I have a couple of different enums. Let's say: enum Beer { Bud, Coors, Michelob } enum Woman { Blonde, Brunnette, Redhead }
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As...
12
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hi, I have create a class name employee. Next, i would like to create array for the employee class. The code as below: dim eply(5) as employee when i call eply(0).name="Khrish", an error...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
13
by: Bill Nguyen | last post by:
Is it possible to create your won XSD to use with .NET based on an XML content? For example the one below: <?xml version="1.0"?> <pcats:FuelsDoc...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.