How to assign values to nested class properties? [duplicate]

I have the following classes that I am trying to assign values to but keep getting an Object reference not set to an instance of an object error.

  public class User
{
    public string Gender { get; set; }
    public int Height { get; set; }
    public string UserID { get; set; }
    public Dress dress { get; set; }
}

public class Dress
{
    public int[,] waist { get; set; }
    public int[,] hips { get; set; }
    public int[,] chest { get; set; }
}

I tried doing:

    User user = new User();
    user.UserID = "name";
    user.Gender = "f";
    user.Height = 180;
    user.dress.waist = new int[1, 1];
    user.dress.hips = new int[1, 1];
    user.dress.chest = new int[1, 1];

I thought I was creating the array's wrong but even if I set user.dress.waist to a string I get the same error. All I need to do at the moment is assign values to User's properties and write them out.

Any guidance is greatly appreciated.



Read more here: https://stackoverflow.com/questions/64180156/how-to-assign-values-to-nested-class-properties

Content Attribution

This content was originally published by Anthony Ryan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: