473,657 Members | 2,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Rea dFromFile("blah blah.txt");
myNamesList.Cap italization = NameCapitalizat ion.AllCaps;
Console.WriteLi ne(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Cap italization = NameCapitalizat ion.ProperName;
Console.WriteLi ne(NamesList[1].FirstName); // Output is "Dan"

What's the magic ingredient I'm missing?
Nov 15 '05 #1
8 1262
Daniel Billingsley <db**********@N O.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.co m>
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**********@N O.durcon.SPAAMM .com> wrote in message
news:uh******** ******@tk2msftn gp13.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.Rea dFromFile("blah blah.txt");
myNamesList.Cap italization = NameCapitalizat ion.AllCaps;
Console.WriteLi ne(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Cap italization = NameCapitalizat ion.ProperName;
Console.WriteLi ne(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**********@N O.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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 68-74-16-211.ded.amerite ch.net 68.74.16.211
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1963 99
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Rea dFromFile("blah blah.txt");
| myNamesList.Cap italization = NameCapitalizat ion.AllCaps;
| Console.WriteLi ne(NamesList[1].FirstName); // Output is "DAN"
| myNamesList.Cap italization = NameCapitalizat ion.ProperName;
| Console.WriteLi ne(NamesList[1].FirstName); // Output is "Dan"
|
| What's the magic ingredient I'm missing?
|
|
|

Nov 15 '05 #4
Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote:
What I'd like to achieve when using the class is something like..

NamesList myNamesList = new NamesList();
myNamesList.Rea dFromFile("blah blah.txt");
myNamesList.Cap italization = NameCapitalizat ion.AllCaps;
Console.WriteLi ne(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Cap italization = NameCapitalizat ion.ProperName;
Console.WriteLi ne(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.WriteLi ne (NamesList.Firs tName[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.co m>
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.WriteLi ne 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote:
What I'd like to achieve when using the class is something like..

NamesList myNamesList = new NamesList();
myNamesList.Rea dFromFile("blah blah.txt");
myNamesList.Cap italization = NameCapitalizat ion.AllCaps;
Console.WriteLi ne(NamesList[1].FirstName); // Output is "DAN"
myNamesList.Cap italization = NameCapitalizat ion.ProperName;
Console.WriteLi ne(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.WriteLi ne (NamesList.Firs tName[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.co m>
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**********@N O.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.WriteLi ne 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Daniel Billingsley <db**********@N O.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.co m>
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
6075
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 small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
27
2972
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 of good examples I came up with the idea of solving the puzzle explained in code below, with the solver optimized so that a state is never analysed more than once. However, I soon realized that this could be done most naturally (for me, at...
2
1496
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, the ASP Classic can make use of the C# business objects without problem, because they were compiled and registered through COM Interop. however i have just reached a point of uncertainty: sets of data. obviously C# has no problem using the...
5
2942
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 final, never installed beta) installed on this box if it makes a difference (I did not install VS Development Web Server as I'm already using the XP web server). I've seen that I need to attach to the native IIS engine, but I don't know what it's...
6
1851
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 if the same classic .asp pages will work on the .NET 2.0 framework with no problems? I was going to start moving towards the .NET technology but since VS 2005 just recently came out I was hoping it's possible to install the .NET 2.0 framework...
2
3501
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 ASP classic any more or if the life cycle of ASP classic is over. I am evaluating a product and I am not sure I want to recommend an ASP classic product that should be migrated by now (I think anyway). Guidance and thoughts are appreciated. ...
8
2415
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 to log in with their user name and password. Well, instead of adding components to the existing classic ASP web application, we could have just put the extended components into a new web application. But then this would require them to log...
0
1572
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 classic Yahoo are having the problem. I use isMymeType function to check the type, can anyone say how the type should be checked for files from classic yahoo? ie. for gmail part.isMimeType("text/plain") or part.isMimeType("multipart/*") will work....
15
2387
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 work(ed) for never had time or money for courses, I am now in the awkward position that I am -still- developing classic ASP. In it, I have become quite resourceful, I wrote a framework using WSC (windows scripting components) to separate logic from...
0
8384
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.