2

Following is my class with aliases List property of class type (MeetingAliases). How to add multiple items to aliases and pass as a parameter along with other TSGetRootObject property data to some method. for eg.

TSGetRootObject ts = new TSGetRootObject(); ts.aliases ???? //How to add data list here ts.name = id; ts.service_type = srvtype; ts = TransfomationSrv.PostData(ts); public class TSGetRootObject { [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public List<MeetingAliases> aliases { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public bool? allow_guests { get; set; } } public class MeetingAliases { [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string alias { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string conference { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string description { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? id { get; set; } } 
2
  • Something like this: ts.aliases = new List<MeetingAliases>(); ts.aliases.Add(new MeetingAliases()); Commented Oct 5, 2016 at 11:01
  • how to add data to MeetingAliases property. Commented Oct 5, 2016 at 11:15

2 Answers 2

2
ts.aliases = new List<MeetingAliases>(); ts.aliases.Add(new MeetingAliases { alias = "Some text", conference = "Other text" }); 

I'd recommend that you have a look at Object Initializer

You can also initialize the aliases list in the constructor of the TSGetRootObject:

public class TSGetRootObject { public TSGetRootObject { aliases = new List<MeetingAliases>(); } //Or in c# 6.0 or higher: public List<MeetingAliases> aliases { get; set; } = new List<MeetingAliases>(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Note that the second initializer example is only available from C#6
@GeoffJames - correct :) will add a comment. Thanks
Thanks @GeoffJames
0

One option how you can do this would be:

Add a constructor to TSGetRootObject where you initialize the List. Then add a new object MeetingAliases to the TSGetRootObject object:

TSGetRootObject ts = new TSGetRootObject(); ts.aliases.Add(new MeetingAliases { alias = "alias", conference = "conference", description = "description", id = 1 }); ts.name = id; ts.service_type = srvtype; ts = TransfomationSrv.PostData(ts); public class TSGetRootObject { [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public List<MeetingAliases> aliases { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public bool? allow_guests { get; set; } public TSGetRootObject(){ aliases = new List<MeetingAliases>(); } } public class MeetingAliases { [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string alias { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string conference { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string description { get; set; } [DataMember] [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? id { get; set; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.