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?
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?