c# - Setting property values in class called from master class -


thank in advance, confusing hell out of me!

i have 2 classes below store values in before converting them json, don't far can't load values them.

    public class mergefieldsreload {     public string fname { get; set; }     public string lname { get; set; }     public string customerid { get; set; }     public string dob { get; set; }     public string cliniccode { get; set; } }  public class datareload {     public string email_address { get; set; }     public string status { get; set; }     public mergefieldsreload merge_fields { get; set; } } 

as can see mergefieldsreload called datareload, in order json.net see sub array (may have wrong name there, feel free correct me).

however when attempting set value of field in mergefieldsreload so

datareload data = new datareload();  data.merge_fields.fname = row["fname"].tostring(); 

i 'system.nullreferenceexception: object reference not set instance of object.' error. don't error 2 fields directly in datareload, mergefieldsreload.

i've no idea why be, have ideas?

the problem you're facing class reference value, meaning should constructed. can solve defining constructor.

public class mergefieldsreload {     public string fname { get; set; }     public string lname { get; set; }     public string customerid { get; set; }     public string dob { get; set; }     public string cliniccode { get; set; } }  public class datareload {     public datareload()     {         merge_fields = new mergefieldsreload();     }      public string email_address { get; set; }     public string status { get; set; }     public mergefieldsreload merge_fields { get; set; } } 

in c# 6.0 can do:

public class mergefieldsreload {     public string fname { get; set; }     public string lname { get; set; }     public string customerid { get; set; }     public string dob { get; set; }     public string cliniccode { get; set; } }  public class datareload {     public string email_address { get; set; }     public string status { get; set; }     public mergefieldsreload merge_fields { get; set; } = new mergefieldsreload(); } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -