For Programmers: Free Programming Magazines  


Home > Archive > Smalltalk > July 2004 > How to get a Class Object From its Name ???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to get a Class Object From its Name ???
seb

2004-07-16, 8:59 pm

Hi,
I look for a way to get an instance of Class from a string.

This string contains the class name (like 'Set') and I would get the
instance of Class representing this class (like Set)...

I'm not sure to be very clear... :-(
In Java, there is a method of the Class 'Class' : Class forName(String
className) doing the same thing I look for in Smalltalk...

Is it possible ? (or do I use an evaluator to do that ?)

Thanks in advance for your answers.
Seb

Sean Malloy

2004-07-17, 3:58 am

I'm sure my method will be shot down (I'm a newbie, and it seems my
approaches are always wrong!)

but off the top of my head:

name := 'Set'.
class := Smalltalk at: name asSymbol.

Squeak and Dolphin both have #asSymbol


"seb" <sebastien.doncker@wanadoo.fr> wrote in message
news:cd9ps4$2fl$1@news-reader5.wanadoo.fr...
> Hi,
> I look for a way to get an instance of Class from a string.
>
> This string contains the class name (like 'Set') and I would get the
> instance of Class representing this class (like Set)...
>
> I'm not sure to be very clear... :-(
> In Java, there is a method of the Class 'Class' : Class forName(String
> className) doing the same thing I look for in Smalltalk...
>
> Is it possible ? (or do I use an evaluator to do that ?)
>
> Thanks in advance for your answers.
> Seb
>



seb

2004-07-17, 3:58 am

Thanks a lot,
I'm a newbie too...

I don't know if this is the good way but it seems to work. I'll use your
solution.

Is there a Smalltalk guru to say this is not the good answers ??? :-)

Sean Malloy wrote:
> I'm sure my method will be shot down (I'm a newbie, and it seems my
> approaches are always wrong!)
>
> but off the top of my head:
>
> name := 'Set'.
> class := Smalltalk at: name asSymbol.
>
> Squeak and Dolphin both have #asSymbol
>
>
> "seb" <sebastien.doncker@wanadoo.fr> wrote in message
> news:cd9ps4$2fl$1@news-reader5.wanadoo.fr...
>
>
>
>


Michel Bany

2004-07-17, 3:58 am

seb wrote:

> Hi,
> I look for a way to get an instance of Class from a string.
>
> This string contains the class name (like 'Set') and I would get the
> instance of Class representing this class (like Set)...
>

In VisualWorks :
mySet := 'Core.Set' asQualifiedReference value new

In Squeak :
mySet := (Smalltalk classNamed: 'Set') new

Michel.
D. Raab

2004-07-22, 8:57 pm

I can't speak for other Smalltalk dialects offhand... but in VisualAge
I used to use:

'Set' asClass.

This reads a bit simpler, but the downside is that at least in VA 6.0,
asClass is in the private method section of EsString. I thought in
earlier editions of VA it was public. The method asClass calls
abrAsClass, which ultimately executes the following code:

Smalltalk classAt: sbString asSymbol.

I believe Smalltalk at: 'Set' asSymbol will finally wind up calling
the above method in VisualAge.

HTH,
Don

seb <sebastien.doncker@wanadoo.fr> wrote in message news:<cd9ps4$2fl$1@news-reader5.wanadoo.fr>...
> Hi,
> I look for a way to get an instance of Class from a string.
>
> This string contains the class name (like 'Set') and I would get the
> instance of Class representing this class (like Set)...
>
> I'm not sure to be very clear... :-(
> In Java, there is a method of the Class 'Class' : Class forName(String
> className) doing the same thing I look for in Smalltalk...
>
> Is it possible ? (or do I use an evaluator to do that ?)
>
> Thanks in advance for your answers.
> Seb

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com