Hi!
On Tuesday 22 May 2007 8:28 pm, Darren White wrote:
Hello
Is there a way to use a string as the name for a object? For example if I have the input string 'fred' from a user input field how do I create a object named userFred or even just fred?
Use asSymbol to convert string to symbol and use it in subclass: method:
BigBoss subclass: 'SmallBoss' asSymbol instanceVariableNames .....
Ehm... I am not sure Darren wanted to know how to programmatically create a new *class*. Darren - when you write "an object named userFred" what do you mean?
Do you mean an object with an attribute (instance variable) holding a name?
Normally you would create a class called say... "User" by editing the class template in the browser to look like this and then press alt-s:
Object subclass: #User instanceVariableNames: 'name age' classVariableNames: '' poolDictionaries: '' category: 'MyClasses'
Here you see that we defined two instance variables - name and age. The above would roughly translate to a table called "User" with two fields (columns), one called name and one called age - if you happen to be a database guy that might get you to understand this.
Then if you add setter and getter methods to the class that look like: ------------ name: aString name := aString ------------- name ^name ---------- ..and same for age - then you can do this:
| freddie | "<- this is declaring a local variable, can be called whatever" freddie := User new. freddie name: 'Fred'. freddie age: 12. freddie
If you now select the above in a workspace and do "inspect it", the code will be run, a new User object is created, we set its two instance variables and then the last line is just a reference to the object so that the inspector knows which object to inspect.
Did this clear things up?
What is the difference between the source file and the image? Does the image use the code from the source file? If I create code dose it live in the image or source file?
A shorter answer here is that the sources file is never modified. We all have the same, it holds "old" source code and you need not worry about it - but you need it somewhere. :)
The image file and the changes file is a pair that should never be separated. The changes file is an incremental log of all your source code modifications. Keep them together at all times. So your code "lives" in both the image (in compiled form) and in the changes file (in ascii form).
regards, Göran