473,804 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5577
There are several ways. You can use Activator.Creat eInstance() or
Type.InvokeMemb er. 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.GetEnt ryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType ("WindowsApplic ation1.frmAbout ");
// Create instance of form
object pc = Activator.Creat eInstance(formT ype);
// 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.GetEnt ryAssembly();
// Get Type of your form
System.Type formType = frmMain.GetType ("WindowsApplic ation1.frmAbout ");

on this

Type formType = typeof(WindowsA pplication1.frm About);

MN> Hello wu,
MN>
MN> See this code. Comments expain how it works
MN>
MN> // Get current assembly
MN> Assembly frmMain = Assembly.GetEnt ryAssembly();
MN> // Get Type of your form
MN> System.Type formType =
MN> frmMain.GetType ("WindowsApplic ation1.frmAbout ");
MN> // Create instance of form
MN> object pc = Activator.Creat eInstance(formT ype);
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.Creat eInstance() or
r> Type.InvokeMemb er. 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/frlrfsystemacti vatorclasscreat einstancetopic. asp
r>
r> http://msdn.microsoft.com/library/de...ary/en-us/cpre
r> f/html/frlrfsystemtype classinvokememb ertopic.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*****@hotmai l.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.Creat eInstance(...)

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
3325
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 threads and each thread checks the queue, receives the message and sends an email. How do I determine the right amount of threads to create? Thanks, S
0
3647
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 datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
4
17028
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 can't seem to create it or call methods to this newly created instance. I have the following code: public class Script {
0
1645
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#) for the same..For that I did the following thing I added a web reference for the service thru "Add Web Reference", It got added successfully, But iam unable to find the web reference in my c# class. Because of that iam not able to invoke the...
5
1566
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
9605
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 System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim WebRequest As HttpWebRequest Dim instance As HttpWebRequest =...
12
1622
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 message prompt out as saying object reference is null...
4
12454
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
13
8169
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 xmlns="http://www.naxml.org/Retail-EDI/Vocabulary/2003-10-16" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="NAXML-FuelPrice15.xsd"> <pcats:TransmissionHeader>
4
6917
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 function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.