473,387 Members | 1,492 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,387 software developers and data experts.

How to call the right constructor

Hi guys. I am trying to create a specific object calling its constructor
based on a string containing the name of the object. Let say I have three
objects A, B, and C. Also let say I let the user enter a text in a textbox.
If the user enters 'A' or 'B' I want to do this

if (TextBox1.Text == 'A')
object O = new A();

if (TextBox1.Text == 'B')
object O = new B();

.....and so on.

Since at design time I dont know what objects I have I need to do this in a
generic and dinamic way...is there any way?? I hope so!
Any help would be great.
Thanks,
Alex.
Feb 27 '06 #1
2 1109
Since at design time I dont know what objects I have I need to do this in a
generic and dinamic way...is there any way?? I hope so!


Check out System.Activator.CreateInstance().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '06 #2
Thanks! that's the way to go, though I am still trying to make it work with
my multiple parameters.

Thanks again,
alex.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Since at design time I dont know what objects I have I need to do this in
a
generic and dinamic way...is there any way?? I hope so!


Check out System.Activator.CreateInstance().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 27 '06 #3

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
9
by: PengYu.UT | last post by:
Hi, I have the following class. I want the constructor gnuplot_2d(int size) has the same behavior as if I call from the 2-argument constructor gnuplot_2d(size, size). But the following...
3
by: Ng Hun Yang | last post by:
I have a class that has a default constructor that initializes the class's variables. Now I want to add a new constructor to skip the initialization. My attempt: class Integer { public: class...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
7
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
4
by: jinendrashankar | last post by:
Hi In my Code i am creating 2 object of my class and in assigentment operator passing arugement by reference. In output of my code i am getting to 2 Constructor call which right but there is...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: James Kanze | last post by:
On Oct 6, 3:59 pm, microcassanova <mangal.pa...@gmail.comwrote: I presume you mean explicit, but what does that change? And what is the relationship with data loss. (Most of my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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...

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.