If it was me, I'd use generics ...
List<Person> myPeople = new List<Person>();
myPeople.Add(new Person("George", "Washington");
Moreover, if you're looking to load up several names, you could ...
public class Persons {
List<Person> mPerson = new List<Person>();
public Persons(params string[] Name) {
for (int n = 0; n < (Name.Length - 2); n += 3) {
mPerson.Add(new Person(Name[n], Name[n + 1], Name[n +
2]));
}
}
public Person this[int Index] { get { return mPerson
[Index]; } }
public int Count { get { return mPerson.Count; } }
}
...
Persons mPersons = new Persons("Fred", "", "Flintstone", "Barney", "",
"Rubble");
Obviously allowing for middle names.
Accessing the array would then be something like ...
for (int n = 0; n < mPersons.Count; n++) {
ListViewItem Item = lv.Items.Add("");
Item.SubItems.Add(mPersons[n].First);
Item.SubItems.Add(mPersons[n].Middle);
Item.SubItems.Add(mPersons[n].Last);
}
On Feb 11, 1:59 pm, Tom <[email protected]> wrote:
> Let's say I have an class called Person which takes two parameters:
> firstName and lastName.
>
> I can instantiate the object like so (c#)
>
> Person myPerson = new Person ("George","Washington");
>
> Now let's say I want to populate an array of objects based on the
> Person class. How do I do this?
>
> Person[] myPeople = new Person[5]; // I have declared an array of
> objects.
> myPeople[0].firstName = "George";
> myPeople[0].lastName = "Washington";
>
> OK, I populated the first occurance of the array of objects, but how
> can I do it on one line like I did when it was not an array?
>
> Thanks in advance for your help.