Monday, 15 April 2013

c# - How to pass values to the list which exist in another class -



c# - How to pass values to the list which exist in another class -

these classes

public class competitor { public string team { get; set; } public string win { get; set; } } public class competitorsclass { public int activecompetitors { get; set; } public list<competitor> competitors { get; set; } public int totalcompetitors { get; set; } public bool haswinodds { get; set; } } public class rootobject { public int eventid { get; set; } public int parenteventid { get; set; } public string mainevent { get; set; } public competitorsclass competitors { get; set; } public string eventstatus { get; set; } public bool issuspended { get; set; } public bool allowbets { get; set; } }

here passing info root class. how can pass values competitorsclass competitors. can tell me how , illustration similar one? know may basic stuff of new c# , learning .

var obj2 = jsonconvert.deserializeobject<dictionary<string, dictionary<string, rootobject>>>(json); foreach (var root in obj2) { int count = root.value.count; foreach (var kid in root.value) { rootobject ob = new rootobject(); ob.eventid = child.value.eventid; ob.eventstatus = child.value.eventstatus; competitorsclass cs = new competitorsclass(); cs.activecompetitors = 0; cs.haswinodds = false; cs.totalcompetitors = 2; // cs.competitors // cs.competitors = ob.competitors; } }

there 2 ways approach problem:

simply assign collection, like:

competitorsclass cs = new competitorsclass(); cs.activecompetitors = 0; cs.competitors = mylist; //or new list<competitor>() if don't have yet.

pass in constructor , assign:

public class competitorsclass { public competitorsclass(list<competitor> competitors) { competitors = competitors; } ... } cs = new competitorsclass(mylist);

the sec preferred, because guarantees variable initialized, instead of relying on default value (null in case).

to existing object's competitors list, write:

ob.competitors.competitors //this "mylist" in examples

c# .net oop

No comments:

Post a Comment