Some general questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Some general questions

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 mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

NorbertHartl
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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

K. K. Subramaniam
In reply to this post by Darren White
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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Some general questions

Ron Teitelbaum
In reply to this post by Darren White
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 mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

Göran Krampe
In reply to this post by K. K. Subramaniam
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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

K. K. Subramaniam
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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

Darren White
In reply to this post by Darren White
Hello, Thanks fro the replies.

I think my first question was a little fuzzy. I think what I need to do as change the question.

If for example I have a seaside web app and users need to create an account. Now, for each user I create an object, say an instance of class Users, which has instance variables such as name and address. So the new user puts in their detail such as name, address, telephone number and then click register. How should (not what) the system name this object. What I'm confused about is I don't know how may users the system will have have do I name all the user object; I can't call them all aUser . If the first user gets an object named user1 how do I create another instance of User named user2 or What about If I want to use one of the input field as part of the name of the object?


The example below may make it clear what I mean

10 timesRepeat: [
x := 1 asString.
''the value of x can be any string object"
user"how do I add x to be part off the name so I end up with variable named user1 user2 ... " := User new.
x := x + x.]

Darren


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Some general questions

Bert Freudenberg

On May 23, 2007, at 16:37 , Darren White wrote:

> Hello, Thanks fro the replies.
>
> I think my first question was a little fuzzy. I think what I need  
> to do as change the question.
>
> If for example I have a seaside web app and users need to create an  
> account. Now, for each user I create an object, say an instance of  
> class Users, which has instance variables such as name and address.  
> So the new user puts in their detail such as name, address,  
> telephone number and then click register. How should (not what) the  
> system name this object. What I'm confused about is I don't know  
> how may users the system will have have do I name all the user  
> object; I can't call them all aUser . If the first user gets an  
> object named user1 how do I create another instance of User named  
> user2 or What about If I want to use one of the input field as part  
> of the name of the object?
>
>
> The example below may make it clear what I mean
>
> 10 timesRepeat: [
> x := 1 asString.
> ''the value of x can be any string object"
> user"how do I add x to be part off the name so I end up with  
> variable named user1 user2 ... " := User new.
> x := x + x.]


There is a cool thing called a "collection" that can hold as many  
users as you want:

        users := OrderedCollection new.
        10 timesRepeat: [users add: User new].

And later you can access an individual user like this:

        users at: 2

        users at: 5

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Some general questions

NorbertHartl
In reply to this post by Darren White
On Wed, 2007-05-23 at 15:37 +0100, Darren White wrote:

> Hello, Thanks fro the replies.
>
> I think my first question was a little fuzzy. I think what I need to
> do as change the question.
>
> If for example I have a seaside web app and users need to create an
> account. Now, for each user I create an object, say an instance of
> class Users, which has instance variables such as name and address. So
> the new user puts in their detail such as name, address, telephone
> number and then click register. How should (not what) the system name
> this object. What I'm confused about is I don't know how may users the
> system will have have do I name all the user object; I can't call them
> all aUser . If the first user gets an object named user1 how do I
> create another instance of User named user2 or What about If I want to
> use one of the input field as part of the name of the object?
>
>
> The example below may make it clear what I mean
>
> 10 timesRepeat: [
> x := 1 asString.
> ''the value of x can be any string object"
> user"how do I add x to be part off the name so I end up with variable
> named user1 user2 ... " := User new.
> x := x + x.]
>
Why not using a collection for this. If I understand you right, you
just need a bunch of user objects. If you think you need some kind
of index just use an OrderedCollection.

users := OrderedCollection new.
10 timeRepeat: [
        users add: User new.
]

Then you have even the chance to access them by index

firstUser := users at: 1

hope this helps,

Norbert

       

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Some general questions

Herbert König
In reply to this post by Darren White
Hello Darren,



DW> 10 timesRepeat: [
DW> x := 1 asString.
DW> ''the value of x can be any string object"
DW> user"how do I add x to be part off the name so I end up with
DW> variable named user1 user2 ... " := User new.
DW> x := x + x.]

if I don't misunderstand you, you want to put your users in a subclass
of Collection, first guess is OrderedCollection. There is no need to
have a single variable with a unique name for each user.

So up to now I don't understand why the system should name the object.

Is it a possibility to say:

users := OrderedCollection new.

Then a user enters his details in an instance of class User and you
put it into the collection users by:

newUser := User new.
user name: ....      "to add the name to the new user"

users add: newUser.  "to stuff this user into the collection of users"

Collection and subclasses offer methods like:

users select: [:eachUser | eachUser name = 'willi']

to give you a collection of all users named 'willi'.

If this points in the right direction read up on collections and their
enumeration methods.

Else hope for someone to understand you better or clarify your
intentions for me :-))



Cheers


Herbert                            mailto:[hidden email]

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Some general questions

Bert Freudenberg
In reply to this post by Bert Freudenberg

On May 23, 2007, at 17:16 , Bert Freudenberg wrote:

>
> On May 23, 2007, at 16:37 , Darren White wrote:
>
>> Hello, Thanks fro the replies.
>>
>> I think my first question was a little fuzzy. I think what I need  
>> to do as change the question.
>>
>> If for example I have a seaside web app and users need to create  
>> an account. Now, for each user I create an object, say an instance  
>> of class Users, which has instance variables such as name and  
>> address. So the new user puts in their detail such as name,  
>> address, telephone number and then click register. How should (not  
>> what) the system name this object. What I'm confused about is I  
>> don't know how may users the system will have have do I name all  
>> the user object; I can't call them all aUser . If the first user  
>> gets an object named user1 how do I create another instance of  
>> User named user2 or What about If I want to use one of the input  
>> field as part of the name of the object?
>>
>>
>> The example below may make it clear what I mean
>>
>> 10 timesRepeat: [
>> x := 1 asString.
>> ''the value of x can be any string object"
>> user"how do I add x to be part off the name so I end up with  
>> variable named user1 user2 ... " := User new.
>> x := x + x.]
>
>
> There is a cool thing called a "collection" that can hold as many  
> users as you want:
>
> users := OrderedCollection new.
> 10 timesRepeat: [users add: User new].
>
> And later you can access an individual user like this:
>
> users at: 2
>
> users at: 5

Also, there is another kind of collection named dictionary that  
allows to use objects other than integers (like strings) to be the key:

        users := Dictionary new.
        1 to: 10 do: [:i | users at: ('user', i asString) put: (User new)]

(I added parens for clarity, they're not necessary in this case)

Retrieval goes like this:

        users at: 'user5'

Cheers,

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Some general questions

Nicolas Cellier-3

Following Bert, the key used to access the dictionary could also be the
instance variable 'name' of the User.
So, if you now that names are unique, you can write:

      users := Dictionary new.
      names := #('Fred' 'Julia' 'Oliver' 'Martha').
      names do: [:aName | | aUser |
        aUser := User new.
        aUser name: aName.
        users at: aUser name put: aUser].
      ^users at: 'Fred'

Of course, if you further change the name of a user, users will get messy...
        (users at: 'Fred') name: 'John'.
        ^users at: 'Fred'.

User 'John' is still registeered at key 'Fred'...

How to update the Dictionary would be another lesson.

If names are not unique, but you still want to access by name, no
problem, you can well make a Dictionary where each entry is a collection
of users sharing same name...

      usersByName := Dictionary new.
      names := #('Fred' 'Julia' 'Oliver' 'Martha' 'Fred').
      names do: [:aName | | aUser |
        aUser := User new.
        aUser name: aName.
        (usersByName at: aUser name ifAbsentPut: [OrderedCollection new])
                add: aUser].
      ^usersByName at: 'Fred'

Then i guesse you will have a birthdate coupled to the name or other
kind of ID to differentiate your users...

After learning a bit of the Collection subclasses, you will see that you
can program as you think.

Nicolas


Bert Freudenberg a écrit :

>
> On May 23, 2007, at 17:16 , Bert Freudenberg wrote:
>
>>
>> On May 23, 2007, at 16:37 , Darren White wrote:
>>
>>> Hello, Thanks fro the replies.
>>>
>>> I think my first question was a little fuzzy. I think what I need to
>>> do as change the question.
>>>
>>> If for example I have a seaside web app and users need to create an
>>> account. Now, for each user I create an object, say an instance of
>>> class Users, which has instance variables such as name and address.
>>> So the new user puts in their detail such as name, address, telephone
>>> number and then click register. How should (not what) the system name
>>> this object. What I'm confused about is I don't know how may users
>>> the system will have have do I name all the user object; I can't call
>>> them all aUser . If the first user gets an object named user1 how do
>>> I create another instance of User named user2 or What about If I want
>>> to use one of the input field as part of the name of the object?
>>>
>>>
>>> The example below may make it clear what I mean
>>>
>>> 10 timesRepeat: [
>>> x := 1 asString.
>>> ''the value of x can be any string object"
>>> user"how do I add x to be part off the name so I end up with variable
>>> named user1 user2 ... " := User new.
>>> x := x + x.]
>>
>>
>> There is a cool thing called a "collection" that can hold as many
>> users as you want:
>>
>>     users := OrderedCollection new.
>>     10 timesRepeat: [users add: User new].
>>
>> And later you can access an individual user like this:
>>
>>     users at: 2
>>
>>     users at: 5
>
> Also, there is another kind of collection named dictionary that allows
> to use objects other than integers (like strings) to be the key:
>
>     users := Dictionary new.
>     1 to: 10 do: [:i | users at: ('user', i asString) put: (User new)]
>
> (I added parens for clarity, they're not necessary in this case)
>
> Retrieval goes like this:
>
>     users at: 'user5'
>
> Cheers,
>
> - Bert -

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners