473,569 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to fire a constructor from a type object

Howdy!
Given that:

Type type = Type.GetType("S ystem.Windows.F orms.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 3623

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

Type type = Type.GetType("S ystem.Windows.F orms.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.GetConstru ctor(Type.Empty Types);

object o = i.Invoke(null);

o should contain the object created by the constructor. Other methods exist,
of course, including Activator.Creat eInstance(), 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.Creat eInstance...
in this case, a simple
object o = Activator.Creat eInstance(type) ;
should create an instance of your type, assuming no strange circumstances.
"Daniel O'Connell" <on******@comca st.net> wrote in message
news:Sk******** **********@rwcr nsc51.ops.asp.a tt.net...

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

Type type = Type.GetType("S ystem.Windows.F orms.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.GetConstru ctor(Type.Empty Types);

object o = i.Invoke(null);

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

exist, of course, including Activator.Creat eInstance(), 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("S ystem.Windows.F orms.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
3074
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 this in any way used to create singletons. Can someone say how? Cheers, Andy
4
7266
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 constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in...
2
3479
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 repopulate the 2nd combo based upon the value of the selected item in the first, just like it does in IE.
45
6323
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { //...
5
6786
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 way is using javascript - does anybody have a sample for this ? Thanks
16
298
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
2987
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 lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm
13
2363
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
3943
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 is much appreciated. JD
0
7703
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...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
0
8138
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...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5223
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...
1
2117
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 we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.