[DataContract]
[Serializable]
public class WSAccount
{
[DataMember]
public string AccountNumber { get; set; }
[DataMember]
public string Pin { get; set; }
}
[DataContract]
[Serializable]
public class WSUser
{
[DataMember]
public WSAccount wsAccount { get; set; }
[DataMember]
public bool IsValidUser { get; set; }
}
Get an error while doing
WSUser user = new WSUser();
user.wsAccount.AccountNumber = userNameresponse.account.ID;
but resolved when
WSUser user = new WSUser();
user.wsAccount = new WSAccount();
user.wsAccount.AccountNumber = userNameresponse.account.ID;
do i need to instantiate child class WSAccount ??
Yes, you do.
That’s because your default constructor (if not redefined, and it’s not) uses default values for every field or auto-property it has. Meaning, every int gets ‘0’ as a value, and every reference type (wsAccount) – null.
So you do have to instantiate wsAccount property to a correct object, otherwise it’s null.