[comp.lang.smalltalk] recognizing instances in Smalltalk/V

dmhouck@c00012-118dan.eos.ncsu.edu (DAVID MICHAEL HOUCK) (08/29/90)

	I'm using Smalltalk/V and am trying to refer to a named instance
variable by way of a string by the same name.  I have tried defining a
method with the message selector identical to the instance variable that
answers the instance variable. For example say my instance variable is
named webster. I defined the method

webster
	^webster

I did this so that when i wanted to access the dictionary webster (an
instance var) in another method of the same class with the string
'webster' i would be referring to that instance var. I should also
mention that i am storing the string in another instance var by way of
selection in a window.  To access the instance webster i have tried such
things like:

     <var holding string 'webster'> asSymbol asociationsDo: ...
     self perform: (<var holding string 'webster'> asSymbol)
associationsDo: ...

but so far i have not been successful in referring to the instance var
webster yet.  But i'm not giving up.  Can any of you experts out there
help me out here. Any advice would be appreciated.  Direct e-mail can be
sent to houck@eceugs.ncsu.edu if you don't want to post the response. Thanks.

jmaloney@cs.washington.edu (John Maloney) (09/05/90)

Suppose object A has the method "webster" defined as:

    webster
        ^webster

Then "A perform: 'webster' asSymbol" should do the trick. If it
didn't work for then perhaps you are sending the "perform:" message
to the wrong object (in your example you sent it to "self" but
maybe "self" wasn't the object that had "webster" defined? Just a
guess.)

The other thing you can do is:
	A instVarAt: index

where "index" is the index of the instance variable "webster" in object A.
You can find the proper index using:

	index := (A class allInstVarNames) indexOf: 'webster'

Using "instVarAt:" does not even require you to have defined the
method for "webster". What you are doing is intentionally breaking
the object encapsulation of Smalltalk. There are times when this is
justified--for example, the debugger and inspector tools use this trick--
but you should be really sure that this is the only way to do what
you want to do. Why not simply keep a dictionary of name->value
bindings in your object and implement a "getValueOf: aName" message
to look up the values?

This info is for Smalltalk-80; ST/V might differ. Good luck.

	-- John