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?
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?
Is there a way to trigger a save prompt when clicking on the window close button?
And a question about posting to this list: I notice that the replies to my last post didn't have 'Re' prefixed to them. How do I reply to a thread. For my lat post it seems that I accidentally relayed to the thread I had started by starting a new thread -oops.
Darren
On Tue, 2007-05-22 at 15:58 +0100, 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?
You can get the class object from a String by using
Smalltalk classNamed: #fred
On this class you can invoke new to get an object.
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?
There are three files: sources, changes, image. The sources file never changes if you are working with the image. The changes file records all source code you enter/alter. The image contains all the compiled source code and the objects which have been created from there. The image is the actual state of your environment which have been dumped to disk.
Is there a way to trigger a save prompt when clicking on the window close button?
I don't know. Usually it is the other way round. You will be asked to close the window discarding unsaved changes.
Norbert
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 .....
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?
The image is your Squeak computer. If you open the System Browser, the last button in the bottom frame of System Browser is set to 'show source'. If you change it to 'bytecodes' you will see instructions for methods. If you set it to 'decompile', then bytecodes are converted to Smalltalk code, but you wont see comments or descriptive parameter names. With 'show source', the browser will pull out readable text from the Sources files. Now if you are a programmer and make any changes to the displayed code, the changes are saved in 'changes' file. Open a 'simple change sorter' to browse through the changes. Now you can pick 'showDiffs' to see a pretty picture of what you have changed. Changes can be filed out into "change sets". These change sets can be filed into another image. The Squeak team aggregates such changesets and merges them to produce the next version of the Image and Sources files.
Squeak's smooth integration of toolchain makes it all appear so easy!
Hope this helps .. Subbu
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
On Wednesday 23 May 2007 6:34 pm, Göran Krampe wrote:
Hi!
On Tuesday 22 May 2007 8:28 pm, Darren White wrote: ..
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?
.. 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?
Goran, you are right. Thanks for catching it.
Darren, apologies for the misdirection.
Subbu
Hi Darren,
Chances are you don't want an object called UserFred. What you want instead is an object with an ivar #name that has the value 'Fred'. Unless Fred is a unique type of object User that can do things that all other Users can not or if Fred is not allowed to do some things that all other users are, you shouldn't think of subclasses as instances of objects. Instances are identified mostly by their content.
So
Object subclass: #User instanceVariableNames: 'userName'
User class >> newForUserNamed: aUserName "create an instance of User and assign aUserName to the userName instance variable" ^self new userName: aUserName; yourself
now you can go to a workspace and do this.
User newForUserNamed: 'Fred'.
If you highlight it and inspect it you will get back a user named 'Fred'.
(User class >> means that the method that you write goes on the class you create called User on the class side [press the class button])
Happy coding!
Ron Teitelbaum ________________________________________ From: Darren White
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?
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?
Is there a way to trigger a save prompt when clicking on the window close button?
And a question about posting to this list: I notice that the replies to my last post didn't have 'Re' prefixed to them. How do I reply to a thread. For my lat post it seems that I accidentally relayed to the thread I had started by starting a new thread -oops.
Darren
beginners@lists.squeakfoundation.org