J88
J88

Reputation: 831

Using a Person class in an array

Hi i have following problem:

I'm creating a program with a array of Person objects.

aantalpersonen = int.Parse(tbArray.Text); // aantal te creëren items in array;

if (aantalpersonen > 5 || aantalpersonen <= 1)
    throw new ArgumentOutOfRangeException();
else
{
    Persoon[] personenLijst = new Persoon[aantalpersonen];
    foreach (Persoon persoon in personenLijst)
    {
         Persoon pers1 = new Persoon();
    }
}

watching this in debugging gives me = a new array with the userdefined number of Persons, an integer with the amount of persons.. In some other lines of code i get an integer wich holds the current chosen (to edit) Person. All this works fine , but when i try to add data to my properties of each Person i get problems.

private void btnUpdateData_Click(object sender, RoutedEventArgs e)
{
    personenLijst[huidigpersoon - 1].Naam = tbNaam.Text;
    personenLijst[huidigpersoon - 1].Gewicht = int.Parse(tbGewicht.Text);
    personenLijst[huidigpersoon - 1].Lengte = int.Parse(tbLengte.Text);
    personenLijst[huidigpersoon - 1].Geboortedatum = dpGeboorte.SelectedDate.GetValueOrDefault(DateTime.Today);
}

this gives me following error :

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object.

My first tought is i have to specificly declare each person by using the ammount of persons the user has chosen. But i can't figger out how i should do this and even if i would manage to , how can i make sure each instance of the personclass has a different name for example persoon1 ,persoon2 ..

Upvotes: 1

Views: 1724

Answers (3)

J88
J88

Reputation: 831

Ok this works, thanks the problem was that i did this : Persoon[] personenLijst = new Persoon[aantalpersonen]; after the else loop, which created a NEW array next to the first i declared at global level

by putting this instead personenLijst = new Persoon[aantalpersonen]; everything works fine! Thanks for your help guys ! Appreciate it

Upvotes: 0

Steve
Steve

Reputation: 216313

You have declared the array to contain a specified number of Persoon objects, but you haven't set any of the elements of the array to be an actual Persoon instance.
So you cannot use something that is not there

aantalpersonen = int.Parse(tbArray.Text); 

if (aantalpersonen > 5 || aantalpersonen <= 1)
    throw new ArgumentOutOfRangeException();
else
{
    Persoon[] personenLijst = new Persoon[aantalpersonen];
    for( int x = 0; x < personenLijst.Length ; x++)
    {
       personenLijst[x] = new Persoon();
    }
 }

After that your array is filled with instances of Persoon class and you can change the properties of the individual instances. However, as noted by Mr Holterman in its comment below, the array is declared as a local variable in this snippet of code, so it is not available (not in scope) in the event handler where you try to change the individual properties. To fix this problem the array should be declared at the global level.

 Persoon[] personenLijst = null;

and initialized with

 personenLijst = new Persoon[aantalpersonen];

Said that, why do you still use arrays instead of a more versatile List<Persoon>

Upvotes: 4

artragis
artragis

Reputation: 3713

Your problem comes from the fact you didn't initialize your array members.

You create a bunch of "Person" but they are not stored into your array. As .NET states, an array is by default initialized with all columns set to null.

Your code should be :

Persoon[] personenLijst = new Persoon[aantalpersonen];
int i = 0;    
   foreach (Persoon persoon in personenLijst)
     {
       personenLijst[i] = new Persoon();
       i++;
     }

Upvotes: 2

Related Questions