473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to solve this classic problem?

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?
Nov 15 '05 #1
8 1250
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
The indexer should clone the object, and then perform the required
capitalisation on the cloned object.
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
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?

Nov 15 '05 #3

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" <db**********@NO.durcon.SPAAMM.com>
| 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: <uh**************@tk2msftngp13.phx.gbl>
| 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!tk2msftn gp13.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?
|
|
|

Nov 15 '05 #4
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Thanks everyone for the replies.

Jon, I did think of a few of those, and like you mention, some of them seem
pretty kludgey to me.

In particular, while option #1 would probably work for this case, I feel
it's a bit constraining of future potential.

Option #2 would seem to work pretty well, even if it ain't real pretty as
you mention. I had hoped for something a bit more elegant. But I'm
wondering what the declaration of the FirstName property would look like - I
mean how do you declare a property to appear to work like an indexer, that
is called with [] instead of ()?

In the clone option, you say there would be a large number of objects
created, but wouldn't the life/scope of the clone be fairly short? I mean
technically it would only exist during the Console.WriteLine lifespan,
right? Or are you saying the GC works in such an opportunistic way that
they would sort of pile up?

All in all I think I like William's posted approach. For obvious reasons, I
did not want to iterate through the entire ArrayList and set a property on
each struct or person class every time the NamesList property is changed. I
think I understand William to argue that from an OO standpoint the Person
class or struct should really contain the capitalization logic. That was
my thinking, and it had me struggling for a solution. His posted code seems
like a good compromise to me.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
I also thought this morning that I could probably make this work by using a
static field/property in the Person class, right? In a very limited and
controlled situation that would meet my needs, but I am also apprehensive
about the constraints that would put on future potential. For example, if I
had two NamesLists, each with its ArrayList of Person classes, then
effectively the capitalization would be the same for both NamesLists.
Right? That's exactly why static variables are so "dangerous", isn't it?
Nov 15 '05 #7
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
Thanks everyone for the replies.

Jon, I did think of a few of those, and like you mention, some of them seem
pretty kludgey to me.

In particular, while option #1 would probably work for this case, I feel
it's a bit constraining of future potential.

Option #2 would seem to work pretty well, even if it ain't real pretty as
you mention. I had hoped for something a bit more elegant. But I'm
wondering what the declaration of the FirstName property would look like - I
mean how do you declare a property to appear to work like an indexer, that
is called with [] instead of ()?
You wouldn't - the property would have to return something which itself
had an indexer. And yes, that's nasty :(
In the clone option, you say there would be a large number of objects
created, but wouldn't the life/scope of the clone be fairly short?
Yup, so it's probably not a problem.
I mean
technically it would only exist during the Console.WriteLine lifespan,
right? Or are you saying the GC works in such an opportunistic way that
they would sort of pile up?
They'd pile up *very* temporarily.
All in all I think I like William's posted approach. For obvious reasons, I
did not want to iterate through the entire ArrayList and set a property on
each struct or person class every time the NamesList property is changed. I
think I understand William to argue that from an OO standpoint the Person
class or struct should really contain the capitalization logic. That was
my thinking, and it had me struggling for a solution. His posted code seems
like a good compromise to me.


Certainly the Person class itself doing the formatting would be OO
goodness.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
I also thought this morning that I could probably make this work by using a
static field/property in the Person class, right?
Yes, although that's not very clean.
In a very limited and
controlled situation that would meet my needs, but I am also apprehensive
about the constraints that would put on future potential. For example, if I
had two NamesLists, each with its ArrayList of Person classes, then
effectively the capitalization would be the same for both NamesLists.
Right? That's exactly why static variables are so "dangerous", isn't it?


Yup. You might also have a think about the nasty consequences of having
multiple threads doing stuff at the same time...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
27
by: Alf P. Steinbach | last post by:
I'm writing a little piece on pointers, to be "bundled" with my attempted correct C++ tutorial (see FAQ item 29.20, yes, I've finally started to at least think about that again), and while thinking...
2
by: jason | last post by:
the enterprise is going to eventually convert the existing ASP Classic website to ASP.NET until that time, development has already begun for a C# library of business objects. for the most part,...
5
by: Velvet | last post by:
Can someone tell me to what process I need to attach to be able to step through my classic ASP code in VS.net 2003. I'm working on an XP box with IIS installed. I also have VS.net 2005 (The...
6
by: John | last post by:
Hello. I believe I've read somewhere that classic .asp pages will still operate correctly with no problems with the .NET 1.1 framework installed on a Windows 2000 Server server. Does anyone know...
2
by: needin4mation | last post by:
I can't find out if Microsoft supports "Classic" asp or if they only support asp.net? I'm not really sure what support means in either context, really, except that they tell customers not to use...
8
by: antonyliu2002 | last post by:
We are extending a web application written in classic ASP long time ago. We will add more components to this web application in ASP.NET 2.0. To use the web application, our web users will have...
0
by: sandeepk84 | last post by:
Hi all... I am facing a problem in getting attachments from Yahoo Classic using JavaMail. My program reads the attachments from gmail and Yahoo Beta. But attachments of type pdf or txt from...
15
by: erik.oosterwaal | last post by:
Hi All, I have been developing websites in classic asp using VB script for a long while now. Due to the fact that I also took a detour to developing ColdFusion, and the fact the companies I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.