[Newbies] Re: Some general questions

nicolas cellier ncellier at ifrance.com
Wed May 23 19:24:07 UTC 2007

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.


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 -

More information about the Beginners mailing list