|
Re: Some general questions: msg#00126lang.smalltalk.squeak.beginners
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 :
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Re: Some general questions, Bert Freudenberg |
|---|---|
| Next by Date: | shortcut to search all categories for a particular class?, Giles Bowkett |
| Previous by Thread: | Re: Re: Some general questions, Bert Freudenberg |
| Next by Thread: | Re: Re: Some general questions, Norbert Hartl |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |