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

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("mercedes") will return mercedes
object and so CreateObject("ford") 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 1338
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

"CreateObject" <ha*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.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("mercedes") will return mercedes
object and so CreateObject("ford") 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.TypeLoadException: Could not load type 'NameSpaceThing.Class1' from
assembly 'App_Web_xcjnboz6, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null'.
Class1 myCla1 = new Class1();
//works fine
object px = Activator.CreateInstance(null, "NameSpaceThing.Class1");
//throws error

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

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>, haber
iletisinde şunları yazdı:ut**************@TK2MSFTNGP02.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

"CreateObject" <ha*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.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("mercedes") will return mercedes
object and so CreateObject("ford") 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.CreateInstance( "NameSpaceThing" , "Class1");

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

"CreateObject" <ha*********@gmail.com> wrote in message
news:eB**************@TK2MSFTNGP02.phx.gbl...
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoadException: Could not load type 'NameSpaceThing.Class1' from
assembly 'App_Web_xcjnboz6, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null'.
Class1 myCla1 = new Class1(); //works fine
object px = Activator.CreateInstance(null, "NameSpaceThing.Class1");
//throws error

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

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>,
haber iletisinde şunları yazdı:ut**************@TK2MSFTNGP02.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

"CreateObject" <ha*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.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("mercedes") will return
mercedes object and so CreateObject("ford") 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.CreateInstance()
method.

these are differen classed stored in different files under different paths
Default.cs
namespace NameSpaceThing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 objCls = new Class1();
object px = Activator.CreateInstance(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.machin AT dot.state.fl.us>, haber
iletisinde şunları yazdı:OO**************@TK2MSFTNGP02.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.CreateInstance( "NameSpaceThing" , "Class1");

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

"CreateObject" <ha*********@gmail.com> wrote in message
news:eB**************@TK2MSFTNGP02.phx.gbl...
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoadException: Could not load type 'NameSpaceThing.Class1'
from assembly 'App_Web_xcjnboz6, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null'.
Class1 myCla1 = new Class1(); //works fine
object px = Activator.CreateInstance(null, "NameSpaceThing.Class1");
//throws error

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

}
}
}


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>,
haber iletisinde şunları yazdı:ut**************@TK2MSFTNGP02.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

"CreateObject" <ha*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.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("mercedes") will return
mercedes object and so CreateObject("ford") 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

"CreateObject" <ha*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.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("mercedes") will return mercedes
object and so CreateObject("ford") 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
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 ~'...
10
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
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...
6
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
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
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
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...
2
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...
62
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...
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
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?
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...
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
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,...
0
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...

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.