473,624 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating object but not using the standart way :(

Assume that I have the classes below;

class mercedes: IAuto {
....
}

class ford: IAuto{
....
}

class audi: IAuto{
....
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("m ercedes") will return mercedes
object and so CreateObject("f ord") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.
May 4 '06 #1
6 1349
Look into the Reflection namespace. Reflection allows you to create
objects and invoke their properties, methods, etc. where their types
are not known at compile time.

May 4 '06 #2
HI,
Of course :)

Take a look at CreateInstance method, two classes implement it, AppDomain &
Activator , depending of what you have ( assembly where they were defined,
complete name, etc) you can use either one.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("m ercedes") will return mercedes
object and so CreateObject("f ord") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.

May 4 '06 #3
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoad Exception: Could not load type 'NameSpaceThing .Class1' from
assembly 'App_Web_xcjnbo z6, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null'.
Class1 myCla1 = new Class1();
//works fine
object px = Activator.Creat eInstance(null, "NameSpaceThing .Class1");
//throws error

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >, haber
iletisinde şunları yazdı:ut******* *******@TK2MSFT NGP02.phx.gbl.. .
HI,
Of course :)

Take a look at CreateInstance method, two classes implement it, AppDomain
& Activator , depending of what you have ( assembly where they were
defined, complete name, etc) you can use either one.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("m ercedes") will return mercedes
object and so CreateObject("f ord") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.


May 4 '06 #4
Hi,

Is the code in the same assembly where you define your class?
from the error message it seems not.

what if you call it like:
object px = Activator.Creat eInstance( "NameSpaceThing " , "Class1");

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:eB******** ******@TK2MSFTN GP02.phx.gbl...
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoad Exception: Could not load type 'NameSpaceThing .Class1' from
assembly 'App_Web_xcjnbo z6, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null'.
Class1 myCla1 = new Class1(); //works fine
object px = Activator.Creat eInstance(null, "NameSpaceThing .Class1");
//throws error

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >,
haber iletisinde şunları yazdı:ut******* *******@TK2MSFT NGP02.phx.gbl.. .
HI,
Of course :)

Take a look at CreateInstance method, two classes implement it, AppDomain
& Activator , depending of what you have ( assembly where they were
defined, complete name, etc) you can use either one.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just
using strings. In other words, CreateObject("m ercedes") will return
mercedes object and so CreateObject("f ord") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.



May 5 '06 #5
My class is not in the same assembly where I call Activator.Creat eInstance()
method.

these are differen classed stored in different files under different paths
Default.cs
namespace NameSpaceThing
{
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Class1 objCls = new Class1();
object px = Activator.Creat eInstance(null,
"NameSpaceThing .Class1");
}
}
}

and my class Class1.cs

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{
...
}
}
}

I also tried what you've suggested but nothing has changed
again :(
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >, haber
iletisinde şunları yazdı:OO******* *******@TK2MSFT NGP02.phx.gbl.. .
Hi,

Is the code in the same assembly where you define your class?
from the error message it seems not.

what if you call it like:
object px = Activator.Creat eInstance( "NameSpaceThing " , "Class1");

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:eB******** ******@TK2MSFTN GP02.phx.gbl...
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoad Exception: Could not load type 'NameSpaceThing .Class1'
from assembly 'App_Web_xcjnbo z6, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null'.
Class1 myCla1 = new Class1(); //works fine
object px = Activator.Creat eInstance(null, "NameSpaceThing .Class1");
//throws error

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >,
haber iletisinde şunları yazdı:ut******* *******@TK2MSFT NGP02.phx.gbl.. .
HI,
Of course :)

Take a look at CreateInstance method, two classes implement it,
AppDomain & Activator , depending of what you have ( assembly where they
were defined, complete name, etc) you can use either one.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just
using strings. In other words, CreateObject("m ercedes") will return
mercedes object and so CreateObject("f ord") will give me the ford
one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.



May 5 '06 #6

"CreateObje ct" <ha*********@gm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("m ercedes") will return mercedes
object and so CreateObject("f ord") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.


A lot of people are going to tell you how to do this with reflection but,
except when using plugins, reflection is not necessary and should be avoided
wherever possible for security, maintainability and correctness reasons.

You can avoid switch statements by using a dictionary to look up a delegate
to construct the appropriate object.

Of course the dictionary has to be built explicitly but at least you know
that you are not going to get a security exception and you know and can
control exactly what can and cannot be instantiated.


May 6 '06 #7

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

Similar topics

2
3568
by: Jörg Maier | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hy, i want remote ls via shh from a windows machine. after starting the command via # stdoutssh = os.popen('/path/to/ssh.exe user@host ls -al ~' -+' 2>&1', 'r') i have the possibility to watch the standart output in the pipe, but i cant give the password. when giving 'w' as second parameter, i just can write, but not read. ssh is installed by cygwin. i dont want to use ssh-keys,
10
1296
by: heinquoi | last post by:
Hello, I have some problemes with microsoft windows programming. Could you help me ? I would the URL of the C++ last standart. thank for your answer. Heinquoi
7
1672
by: Lloyd Dupont | last post by:
I'm about to buy VS.NET2005 standart edition which fits my need very well. What would be better though (cheaper and same feature, but I haven't tried) is to buy C++ express & C# express. If I buy both of these, could I have a mixed project (and debugging) experience with native C++, .NET C++ & C#, all together? Or do I need to go for the standart edition? -- Regards, Lloyd Dupont
6
9560
by: n_o_s_p_a__m | last post by:
Ok, here is a dumb one: Is there any difference between the lines new object() and new Object()
0
886
by: Alex Sedow | last post by:
Look at example: namespace N { class A { public void v() {}} class B : A { new public const int v = 10; } class C : B { new public void v ( int a ) {}} class D : C { delegate void fun();
4
1252
by: David | last post by:
hello... i coded these: public class TextBoxBrowse : TextBox { public TextBoxImgBrowse () : base () { Button b = new Button(); b.Size = new System.Drawing.Size(24, 21);
15
6742
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
2
1109
by: Lloyd Dupont | last post by:
When .NET 2.0 get out I ordered VS2005 Standart edition and downloaded all the express line. + the platform SDK. I had to modify some VS environment setting IN VCExpress so it could find the .lib files & .h files (in the platform SDK). Friday I recieved VS2005 standart and installed it. Sunday night I tried to compile some C utility I had written. It didn't, with: 'error spawning CL.exe'
62
4328
by: Laurent Deniau | last post by:
I just put the draft of my paper on the web: http://cern.ch/laurent.deniau/html/cos-oopsla07-draft.pdf I would be interested by any feedback from C programmers (with little OO knowledge) to improve the paper quality, even if I don't know yet if the paper will be accepted. It is not intended to be a manual nor an introduction to OOP. Just to mention it (not in the paper), my programming background is 10+ years of C/C++.
0
8170
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8474
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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...
0
5561
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
4078
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...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
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
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.