1

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

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Josh, in addition to @SLaks's answer, you can also query the List<Person> using LINQ if you are developing in 3.5.
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 - You can accept an answer by clicking the tick to the left of the answer to green. This is how SO works.
2

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.

6 Comments

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, one could also say that LINQ is unnecessary. I like this approach.
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.
@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))
@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.
|
1

You can do this quite compactly with Linq:

var xml = XDocument.Load("persons.xml");

var persons =   // person == IEnumerable<Person>
      from P in xmlDescendants("person")
      select new Person()    // your Person class
      {
           Name = P.Element("name").Value,
           Age = P.Element("age").Value
      };

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.