How to solve this classic problem?

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

I've got to believe this is a fairly common / classic problem, and I think
I've even read an example somewhere in my education thus far, but I sure
can't remember it.

Suppose I want to store a list of first and last names read from a file. I
think somewhere I need an ArrayList. So I have a class that stores a
private ArrayList.

I want this class to be able to return the first name, the last name, or a
"calculated" FullName... but I want to be able to set a property of this
class of say Capitalization and have it affect the results of accessing the
names. I realize it's easy enough to have a class implement an indexer that
returns the proper element of say an ArrayList of structures, but I need to
modify each part of the structure upon it's access. That would be easy
enough to do in the accessors in the structure, but the structure doesn't
really know anything about the class's Capitalization property, and I really
want to avoid having the structure also hold a Capitalization property and
having to go through the entire ArrayList every time the Class property is
changed.

I realize I could also code a GetLastName(int index) or something like that
in the class, but that seems a bit kludgey and wouldn't be as
straightforward to the users of the class as I would hope to achieve. What
I'd like to achieve when using the class is something like..

NamesList myNamesList = new NamesList();
myNamesList.ReadFromFile("blahblah.txt");
myNamesList.Capitalization = NameCapitalization.AllCaps;
Console.WriteLine(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Capitalization = NameCapitalization.ProperName;
Console.WriteLine(NamesList[1].FirstName); // Output is "Dan"

What's the magic ingredient I'm missing?
 
Daniel Billingsley said:
I've got to believe this is a fairly common / classic problem, and I think
I've even read an example somewhere in my education thus far, but I sure
can't remember it.

<snip>

I'll have a closer look at this tomorrow - it looks like an interesting
problem. I won't be able to post an answer for about 18 hours though -
but I thought I'd just post to say I *will* be looking at it later :)
 
The indexer should clone the object, and then perform the required
capitalisation on the cloned object.
 
Hi Daniel,

Thanks for posting in this group.
I think the copy data method is not a good performance way, while William's
way is suitable.
His method is checking the Capitalization property in each property's get
accessor, then you can return the suitable property format.

If you have anything unclear, please feel free to let us know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Daniel Billingsley" <[email protected]>
| Subject: How to solve this classic problem?
| Date: Mon, 3 Nov 2003 16:11:37 -0500
| Lines: 35
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 68-74-16-211.ded.ameritech.net 68.74.16.211
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196399
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I've got to believe this is a fairly common / classic problem, and I think
| I've even read an example somewhere in my education thus far, but I sure
| can't remember it.
|
| Suppose I want to store a list of first and last names read from a file.
I
| think somewhere I need an ArrayList. So I have a class that stores a
| private ArrayList.
|
| I want this class to be able to return the first name, the last name, or a
| "calculated" FullName... but I want to be able to set a property of this
| class of say Capitalization and have it affect the results of accessing
the
| names. I realize it's easy enough to have a class implement an indexer
that
| returns the proper element of say an ArrayList of structures, but I need
to
| modify each part of the structure upon it's access. That would be easy
| enough to do in the accessors in the structure, but the structure doesn't
| really know anything about the class's Capitalization property, and I
really
| want to avoid having the structure also hold a Capitalization property and
| having to go through the entire ArrayList every time the Class property is
| changed.
|
| I realize I could also code a GetLastName(int index) or something like
that
| in the class, but that seems a bit kludgey and wouldn't be as
| straightforward to the users of the class as I would hope to achieve.
What
| I'd like to achieve when using the class is something like..
|
| NamesList myNamesList = new NamesList();
| myNamesList.ReadFromFile("blahblah.txt");
| myNamesList.Capitalization = NameCapitalization.AllCaps;
| Console.WriteLine(NamesList[1].FirstName); // Output is "DAN"
| myNamesList.Capitalization = NameCapitalization.ProperName;
| Console.WriteLine(NamesList[1].FirstName); // Output is "Dan"
|
| What's the magic ingredient I'm missing?
|
|
|
 
Daniel Billingsley said:
What I'd like to achieve when using the class is something like..

NamesList myNamesList = new NamesList();
myNamesList.ReadFromFile("blahblah.txt");
myNamesList.Capitalization = NameCapitalization.AllCaps;
Console.WriteLine(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Capitalization = NameCapitalization.ProperName;
Console.WriteLine(NamesList[1].FirstName); // Output is "Dan"

What's the magic ingredient I'm missing?

A few options:

1) Each name could have a reference to the NamesList, and interrogate
that as to which capitalisation style to use.

Pros: Works as per the above code.
Cons: Any name could only be a member of one list; you'd need the extra
reference for every element; the name would need to know about the list
(which you may not want).

2) You could change the code to:

Console.WriteLine (NamesList.FirstName[1]) and make the FirstName
property always return an appropriate object with an "int" indexer
which then asks the list what kind of capitalisation to use, and then
gets the appropriate name.

Pros: The name element is unchanged; it's cheap in memory; there's no
real tie between a name and the list containing it.
Cons: You'd need to change the client code; it feels pretty ugly; the
implementation wouldn't be very nice.

3) You could make your indexer return a clone of the name (or an
encapsulated copy of the name reference), with an appropriate
capitalisation flag, rather than the actual name.

Pros: Pretty clean - same pros as option 2.
Cons: Creates more objects than you probably want.


These are pretty much the options presented in the other posts as well,
I think, apart from 2) which might be a "new" one.
 
Back
Top