views:

90

answers:

3

Hey Stack Overflow'ers,

I'm stuck in a C# program I'm developing, and I think it's because I am taking the wrong approach.

You see, I have these settings in an XML file, which are read at runtime. For each setting, I want to create a new object that does stuff.

I think I can do that, but the problem is, how do I reference those objects?

For example:

  <person>
    <name>Jared</name>
    <age>28</age>
  </person>
  <person>
    <name>Nicole</name>
    <age>32</age>
  </person>

Normally, I'd just do something like:

Person Jared = new Person();

but I have no idea how many people there will be in the XML file. Then later if I want to set/get Jared's age, I would have no idea how to reference it.

Am I missing how to dynamically create objects using OOP?

-Josh

+6  A: 

You need to create a List<Person> instance, which can store zero or more Person objects.

For example:

List<Person> people = new List<Person>();

people.Add(new Person(...));
people.Add(new Person(...));

Console.WriteLine(people[1].Age);

You can also add and read the list using for or foreach loops.

SLaks
@Josh, in addition to @SLaks's answer, you can also query the List<Person> using LINQ if you are developing in 3.5.
AJ
Thanks for the feedback, this is exactly what I was trying to figure out. I also appreciate the LINQ discussion below, though I'll just stick to this for now.
Josh
@Josh - You can accept an answer by clicking the tick to the left of the answer to green. This is how SO works.
Danny Chen
@Danny -- got it. Thanks man.
Josh
A: 

You'll want to put these into a collection, such as a List<T>:

List<Person> people = new List<Person>();

people.Add(new Person("Jared", 28));
people.Add(new Person("Nicole", 31));

For details, refer to the discussion for Collection Classes on MSDN.

Reed Copsey
+2  A: 

A Dictionary<string,Person> may be appropriate for the situation. That way, when you want to reference a person, you could do so by making the person's name the key.

jlafay
this is entirely unnecessary, a person would surely store it's name internally and thus could be retrieved from the list with a linq query
Jean-Bernard Pellerin
@Jean-Bernard Pellerin, one could also say that LINQ is unnecessary. I like this approach.
Fosco
The dictionary might be better for this developers as he seems to be a bit green. Not sure he should jump into LINQ at the moment, but that's only my opinion and perspective.
jlafay
@Jean-Bernard: However, if you're usually retrieving by name, a Dictionary lookup will be much more efficient than a linq query over a List (O(1) instead of O(n))
Reed Copsey
@Jean and why would you want to potentially iterate the entire list (when the sought object is the last) when you can have O(1) lookup? and have code that reads nicely people["Jared"].Age = 18; is easy to read people.First(p => p.Name == "Jared").Age = 18 ain't that straight forward to me.
Rune FS
I've never been a fan of identifying an object by something other than a constant primary key. What happens when someone's name changes? The dictionary and Person at this point become too tightly coupled.
Jean-Bernard Pellerin