473,396 Members | 2,099 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,396 software developers and data experts.

How to fire a constructor from a type object

Howdy!
Given that:

Type type = Type.GetType("System.Windows.Forms.Button");

Question: How do I obtain a Button object by only knowing
the type object. In other words, how do I fire the
constructor of a type object (Button(), in this case) ???

Thank you and God bless.

_____
Marco
Nov 15 '05 #1
3 3601

"Marco" <a@b.c> wrote in message
news:1f****************************@phx.gbl...
Howdy!
Given that:

Type type = Type.GetType("System.Windows.Forms.Button");

Question: How do I obtain a Button object by only knowing
the type object. In other words, how do I fire the
constructor of a type object (Button(), in this case) ???

Thank you and God bless.
Basically:

ConstructorInfo i = type.GetConstructor(Type.EmptyTypes);

object o = i.Invoke(null);

o should contain the object created by the constructor. Other methods exist,
of course, including Activator.CreateInstance(), which are probably eaiser
to use unless you need to verify a certain constructor exists. _____
Marco


Nov 15 '05 #2
of course, it would help if I mentioned how to use
Activator.CreateInstance...
in this case, a simple
object o = Activator.CreateInstance(type);
should create an instance of your type, assuming no strange circumstances.
"Daniel O'Connell" <on******@comcast.net> wrote in message
news:Sk******************@rwcrnsc51.ops.asp.att.ne t...

"Marco" <a@b.c> wrote in message
news:1f****************************@phx.gbl...
Howdy!
Given that:

Type type = Type.GetType("System.Windows.Forms.Button");

Question: How do I obtain a Button object by only knowing
the type object. In other words, how do I fire the
constructor of a type object (Button(), in this case) ???

Thank you and God bless.
Basically:

ConstructorInfo i = type.GetConstructor(Type.EmptyTypes);

object o = i.Invoke(null);

o should contain the object created by the constructor. Other methods

exist, of course, including Activator.CreateInstance(), which are probably eaiser
to use unless you need to verify a certain constructor exists.
_____
Marco


Nov 15 '05 #3
Marco, there are a couple of ways to do this. One way is to use the
GetConstructor(s) methods off of the Type object. You can then call
Invoke() on the ConstructorInfo object.

--
Greg Ewing [MVP]
http://www.citidc.com

"Marco" <a@b.c> wrote in message
news:1f****************************@phx.gbl...
Howdy!
Given that:

Type type = Type.GetType("System.Windows.Forms.Button");

Question: How do I obtain a Button object by only knowing
the type object. In other words, how do I fire the
constructor of a type object (Button(), in this case) ???

Thank you and God bless.

_____
Marco

Nov 15 '05 #4

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

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
2
by: mark4asp | last post by:
Why does this not work in Mozilla ? <http://homepage.ntlworld.com/mark.pawelek/code/animals.html> The optHabitat_change() event does not fire. What am I doing wrong here? PS: It should...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
16
by: Craig Buchanan | last post by:
If I am trying to cast an object to one of its decendants, will the decendants' default constructor (New sub) fire? Thanks, Craig
3
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
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:
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
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.