473,473 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating objects at runtime

Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could be
the actual type:

AddControl( myButton, Button, new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ), "Dummy
1" );
Thx!
- Kimmo Laine
Nov 15 '05 #1
4 3480
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could be
the actual type:

AddControl( myButton, Button, new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ), "Dummy 1" );
Thx!
- Kimmo Laine


Nov 15 '05 #2
Hi,

don?t work

Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );

- Kimmo Laine
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:#9**************@TK2MSFTNGP12.phx.gbl...
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s,
Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( .... ), "Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could be the actual type:

AddControl( myButton, Button, new Size( ...), new Point( ... ),
"Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ),

"Dummy
1" );
Thx!
- Kimmo Laine

Nov 15 '05 #3
Kimmo,

This doesn't work because you are making the call to GetType outside of
the assembly that the type is in. In the documentation for the GetType
method on the Type class, it will tell you how to get the fully-qualified
type name, which is what you need to use for types contained in assemblies
outside of the one where the call to GetType is made.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi,

don?t work

Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );

- Kimmo Laine
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:#9**************@TK2MSFTNGP12.phx.gbl...
Hi Kimmo,

You can use the following classes and methods:

Assembly.CreateInstance(...) - this one accepts full type name
Activator.CreateInstance(...) - this one accepts a Type instance
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
Hello,

(how) can i do the following (in C#)?

// . . .
class MyForm : Form {
// . . .

protected Button myButton = null;
protected TextBox myTB = null;

protected void AddControl( ref Control ctrl, string strType, Size s, Point pt, string txt ) {

ctrl = new < ?!?! >;
ctrl.Location = pt;
ctrl.Size = s;
ctrl.Text = txt;

Controls.Add( ctrl );

}

protected override void OnLoad( EventArgs e ) {

AddControl( myButton, "Button", new Size( ...), new Point( ... ), "Dummy 1" );
AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
"Dummy 1" );

base.OnLoad( e );
}
}

It would be even better if the second parameter ( in AddControl ) could
be
the actual type:

AddControl( myButton, Button, new Size( ...), new Point(

.... ), "Dummy 1" );
AddControl( myTB, TextBox, new Size( ...), new Point( ... ),

"Dummy
1" );
Thx!
- Kimmo Laine


Nov 15 '05 #4
Kimmo Laine <reply.to@newsgroup> wrote:
Type t = Type.GetType( "System.Windows.Forms.Button" );
Button b = (Button)Activator.CreateInstance( t );


When Type.GetType is only given the name of the type without
assembly/version information, it only looks in the currently executing
assembly and mscorlib. If you want it to look elsewhere, you should
either provide the assembly/version information, or use
Assembly.GetType instead on the appropriate assembly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

2
by: ikl | last post by:
When creating a list of objects of the same class, what should be concerned to decide if using "new" or not? Since how many number of the objects are unknown until runtime, probably it is not a...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
3
by: Seth | last post by:
I have been trying to create a mock httpcontext for the purpose of unit testing. I have a class, Customer, that uses cookies. I have set it up to be able to take a httpcontext as a parameter in...
1
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows,...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
3
by: Tyranno.Lex | last post by:
I am using Visual Studio .NET 2003 and have successfully deployed a commercial web application written in C# and ASP.NET. I am now wanting to add reporting using Crystal Reports and am having a...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
1
by: sean.j.gage | last post by:
Is there a way to get to a Type definition from a wsdl through c# during runtime? I'd like to load and process the wsdl into its various objects at run time and be able to use a factory to return...
6
by: gaya3 | last post by:
Hi all, only during runtime , i know the class for which i need to create the object.How to generate objects for different classes at run time?? Please do needfull.. -Hamsa
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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,...
1
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
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.