2

I've created a class

public partial class Subrank
{
    private System.DateTime startDateField;

    public System.DateTime StartDate
    {
        get
        {
            return this.startDateField;
        }
        set
        {
            this.startDateField = value;
        }
    }
}

im then trying to create an array of this...

Subrank[] pastSubRank = new Subrank[1];
pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");

but pastSubRank[0] is crashing saying it is NULL....why is this?

John
  • 3,965
  • 21
  • 77
  • 163

3 Answers3

6

Yes cause you haven't instantiated the class Subrank before accessing it's property.

Subrank[] pastSubRank = new Subrank[1];
pastSubRank[0] = new Subrank();
pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");
Rahul
  • 76,197
  • 13
  • 71
  • 125
4

You need to create an object to put in the array before accessing property

        Subrank[] pastSubRank = new Subrank[1];
        pastSubRank[0] = new Subrank();
        pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");
Tallon
  • 41
  • 4
2
Subrank[] pastSubRank = new Subrank[]
{
    new Subrank() { StartDate = DateTime.Parse("2012-05-22") }
};
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28