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

class instance from string

Greetings,

I have a String instance which contains the name of a class. Is there
some way I can instantiate that class from given the class name in the
string?

Many Thanks, in advance, for any assistance,

Kristoph

Mar 17 '07 #1
4 10424
Kristoph wrote:
Greetings,

I have a String instance which contains the name of a class. Is there
some way I can instantiate that class from given the class name in the
string?
Probably. Need more info, though. Class as in CSS, or Class as a js concept?
"Instantiate"? In what context?
So many questions...
Mick
Mar 17 '07 #2
I have a String instance which contains the name of a class. Is there
some way I can instantiate that class from given the class name in the
string?

If you are trying to do what I think you are, there's a few ways you
can do it.

Here's two examples:

// set up the class
function MyClass() {
this.foo = "bar";
}

// store the class name in a string
var classIdString = "MyClass";

// first method
var classInstance = new this[classIdString];

// second method
var classInstance2 = eval("new "+ classIdString);

// test them
alert(classInstance.foo);
alert(classInstance2.foo);

Mar 18 '07 #3
On Mar 17, 5:02 pm, "Geoff Stearns" <g...@deconcept.comwrote:
I have a String instance which contains the name of a class. Is there
some way I can instantiate that class from given the class name in the
string?

If you are trying to do what I think you are, there's a few ways you
can do it.

Here's two examples:

// set up the class
function MyClass() {
this.foo = "bar";

}

// store the class name in a string
var classIdString = "MyClass";

// first method
var classInstance = new this[classIdString];

// second method
var classInstance2 = eval("new "+ classIdString);

// test them
alert(classInstance.foo);
alert(classInstance2.foo);
var classInstance = this[classIdString];

.... that's exactly what I was looking for

Thank you very much.

]{

Mar 18 '07 #4
Kristoph wrote on 18 mrt 2007 in comp.lang.javascript:
On Mar 17, 5:02 pm, "Geoff Stearns" <g...@deconcept.comwrote:
>var classInstance = new this[classIdString];
>
var classInstance = this[classIdString];

... that's exactly what I was looking for
May be, but that was not what was suggested

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 18 '07 #5

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

Similar topics

8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
5
by: Joe Van Dyk | last post by:
Say I have the following class: using std::string; class Player { public: Player() : name(""), age(""), other_stuff("") {} private: string name; string age;
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
6
by: Grok | last post by:
In writing a class library, one of the classes should only ever be instantiated once, and its object should be accessible to every other class in the library. How can I do that? To provide some...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
4
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to...
5
by: (2b|!2b)==? | last post by:
I would like to know if I can specialize only a specific method for a class template. Is the (specialization) code below valid? template <typename T1, typename T2> class MyClass { public:
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: 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
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
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...
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.