Access property inside another model

Viewed 88

I have a simple model inside another model like this:

First Model

public class ReminderPushNotificationTest
{
    public string UserName { get; set; }
    ....
    public ReminderPushNotificationAndroidResult AndroidResult{ get; set; }
}

Second model

public class ReminderPushNotificationAndroidResult
{
    public string Headers { get; set; }
    public string Content { get; set; }
}

When I try to fill model I try something like:

var testModel = new ReminderPushNotificationTest
{
    UserName = username,
    AndroidResult.{Want property inside this model} = result.Header
}

How can I access that property to assign it?

2 Answers
var testModel = new ReminderPushNotificationTest
{
    UserName = username,
    AndroidResult = new ReminderPushNotificationAndroidResult
    {
        Headers = result.Header
    }
}

You can't set a property for an object that hasn't yet been instantiated.

You need to create an instance of second model before filling it's properties likewise you have created instance for the base model class

var testModel = new ReminderPushNotificationTest
{
   UserName = username,
   AndroidResult = new ReminderPushNotificationAndroidResult
   {
       Headers = result.Header,
       Content = "abcd"  
   }
}
Related