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

Type, GetType(), and more.....

Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. I will explain
it....

Lets say I have a class BaseClass, and from this class I derived 5 classes
Derived_1, Derived_2,.....,Derived_5. All these classes are already defined
in a library.

Now, I want to create Derived_2 class just by its name string "Derived_2".
One possible work arround this problem is, to add a suffix to each derived
class, like MyDerived_1, MyDerived_2,..... and then use the TokenPasting to
create a class.

#define CLASS_NAME(__CLS__) My##__CLS__

and use it like this

BaseClass *derived = new CLASS_NAME("Derived_2")();

This works fine, but here you have to passed actual string literal, and I
want to pass a string variable, lets say, strDerivedClassName. This way
token-Pasting will not work. I have seen that, when you create a __gc
object, it has a method, ToString(), which returns class name as string.

public __gc class Person { };
Person *p = new Person();
p->ToString(); //prints Person as string.

Is there any way to create a class instance, which is already defined
somewhere, with its name as a string ?

Thanks in Advance.
Nov 17 '05 #1
4 969
Lord2702 <Lo******@MSN.com> wrote:
Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. [...]


Is that C++ or .NET?
C++ lacks the sophisticated reflection to
do this with what's built into the language,
but there are ways to do this.
For example, you could setup a map of strings
to functions that create the repsective
objects.
Use google to find the Factory Pattern.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Nov 17 '05 #2
Dear Hendrick,
Thanks, for your reply.

In C++ you have RTTI, which also returns typeid info, but, my this question
is for .Net 2003

Yes with map it is possible, but in that case I have to create my Derived
objects and then stored into the map object, this sounds like if I have 20
Derived object, all I have to create and store them in Map object, with
their name.

"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:OC*************@TK2MSFTNGP09.phx.gbl...
Lord2702 <Lo******@MSN.com> wrote:
Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. [...]


Is that C++ or .NET?
C++ lacks the sophisticated reflection to
do this with what's built into the language,
but there are ways to do this.
For example, you could setup a map of strings
to functions that create the repsective
objects.
Use google to find the Factory Pattern.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Nov 17 '05 #3
> public __gc class Person { };
Person *p = new Person();
p->ToString(); //prints Person as string.


If you are using just .NET and __gc classes, you can use
System.Activator.CreateInstance() for this... That's what it is for..

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4
Lord2702 <Lo******@MSN.com> wrote:
Dear Hendrick,
Thanks, for your reply.

In C++ you have RTTI, which also returns typeid info, but, my this question
Yes, but that cannot be used to create
objects unless you put some code around
it for yourself.
but, my this question
is for .Net 2003
I have no idea of this.
Yes with map it is possible, but in that case I have to create my Derived
objects and then stored into the map object, this sounds like if I have 20
Derived object, all I have to create and store them in Map object, with
their name.
You wouldn't have to put those objects
into the map, but functors which create
them. This could be plain C function
pointers or function objects.
[...]

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #5

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

Similar topics

0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
16
by: Dennis | last post by:
I have a class named "myclass" and an arraylist containing elements of type "MyClass". I want to get the value of a property of "MyClass" (a string type) for one of the arraylist elements. I...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
6
by: Paul | last post by:
I have two projects in one solution. One is called Frontier and holds all my base user controls, classes, etc. that are used over multiple applications. The second is my application project...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
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: 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
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
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.