Nick
Nick

Reputation: 533

C# Nested Properties initialize

I have the below simple declaration of nested properties (simplified):

public class standardMessage
{
    public messageProperties message { get; set; }
    public messageFlags flag { get; set; }
}

public class messageProperties
{
    public string messageSubject { get; set; }
    public string messageBody { get; set; }
}

public class messageFlags
{
    public Boolean flagImportant { get; set; }
    public Boolean flagPersonal { get; set; }
}

Upon initialization, I am trying to enter values, but seems I am missing something:

public class Program
{
    static void Main(string[] args)
    {
        standardMessage myMessage = new standardMessage();
        messageProperties myMsgProperties = new messageProperties();

        myMsgProperties.messageSubject = "Hey!";
        myMsgProperties.messageBody = "Howdy";

        //below code throws error System.NullReferenceException: 'Object reference not set to an instance of an object.'
        //MyMessage.message.messageSubject = "Greetings";
        //MyMessage.message.messageBody = "Happy weekend";

        //error - how do I print the values?
        Console.WriteLine(myMessage.message.messageSubject.ToString());
        Console.ReadLine();
    }
}

Could you please help me with the above? wither way I tried, it doesn't run

Upvotes: 0

Views: 206

Answers (2)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

You should initialize the instance of messageProperties first, before accessing the instance properties

myMessage.message = new messageProperties
{
    messageBody = "Happy weekend",
    messageSubject = "Greetings"
};

instead of this

//below code throws error System.NullReferenceException: 'Object reference not set to an instance of an object.'
//MyMessage.message.messageSubject = "Greetings";
//MyMessage.message.messageBody = "Happy weekend";

Then you'll be able to print it out successfully

Console.WriteLine(myMessage.message.messageSubject);

prints

Greetings

Another option is to initialize nested properties in constructor, like that

public class standardMessage
{
    public standardMessage()
    {
        message = new messageProperties();
        flag = new messageFlags();
    }

    public messageProperties message { get; set; }
    public messageFlags flag { get; set; }
}

Then you can assign the values like you want

myMessage.message.messageSubject = "Greetings";
myMessage.message.messageBody = "Happy weekend";

Or you can finish you snippet and set the nested property directly

standardMessage myMessage = new standardMessage();
messageProperties myMsgProperties = new messageProperties();

myMsgProperties.messageSubject = "Hey!";
myMsgProperties.messageBody = "Howdy";

myMessage.message = myMsgProperties;

Console.WriteLine(myMessage.message?.messageSubject);

Prints

Hey!

Null-conditional operator ? will help you to avoid NullReferenceException if any

Upvotes: 1

Tanveer Badar
Tanveer Badar

Reputation: 5523

You need to modify the definition for your standardMessage class to either use inline initializers or add a default constructor (whichever works depending upon the language version you are using).

public class standardMessage
{
    public standardMessage()
    {
        message = new messageProperties();
        flag = new messageFlags();
    }

    public messageProperties message { get; set; } = new messageProperties();
    public messageFlags flag { get; set; } = new messageFlags();
}

Upvotes: 1

Related Questions