How to add the object to a collection from another object?
Clash Royale CLAN TAG#URR8PPP
How to add the object to a collection from another object?
How do I add objects to an ICollection property? Do I need to initialize the ICollection first? If so how?
I have a Class 'foo' that has an ICollection<bar>
property named 'bar'. I also have a separate Collection of objects containing deserialized excel data. I want to populate the ICollection<bar> bar
property using this data.
ICollection<bar>
ICollection<bar> bar
Assuming I have an instance of foo
called 'fooexample' then I am trying to assign a value to the ICollection<bar> bar
by using 'fooexample.bar.id' but I know that this is syntactically incorrect.
foo
ICollection<bar> bar
Class foo
public class foo
public int id get; set;
public string name get; set;
public ICollection<bar> bar get; set;
Class bar
public class bar
public int id get; set;
public string name get; set;
Adding objects from another object
exceldata = ParseDocument.Parse(filestream);
foo fooexample = new foo();
foreach (var object in exceldata)
fooexample.id = object.id;
fooexample.bar.id = object.bar.id;
// <-- how do I do this? if I need to initialize it, how?
It would help understanding if we new how
exceldata
is declared.– Olivier Jacot-Descombes
Aug 10 at 14:53
exceldata
"fooexample.bar" is of type ICollection and has no property "id", so what you're writing is syntactically wrong. You could do "fooexample.bar.Add(x)" and - if "object.bar" is of type "bar", it could be "fooexample.bar.Add(object.bar)". This would get you a collection of all exceldata's "bar" properties. If that's what you asking for.
– Jürgen Röhr
Aug 10 at 14:54
You need:
public ICollection<bar> bar get; = new List<bar>();
and then fooExample.bar.Add(new bar id = obj.id, name = obj.bar.name );
– Olivier Jacot-Descombes
Aug 10 at 17:42
public ICollection<bar> bar get; = new List<bar>();
fooExample.bar.Add(new bar id = obj.id, name = obj.bar.name );
Because you do not intitialize
ICollection<bar> bar
. Change the declaration to public ICollection<bar> bar get; = new List<bar>();
to create a collection. fooexample.bar.id
does not work, because fooexample.bar
is a collection. You do have to access item of the collection. E.g.: fooexample.bar[0].id
– Olivier Jacot-Descombes
Aug 10 at 18:01
ICollection<bar> bar
public ICollection<bar> bar get; = new List<bar>();
fooexample.bar.id
fooexample.bar
fooexample.bar[0].id
1 Answer
1
I get the impression you dont actually need a collection so you could do the following.
Class foo
public class foo
public int id get; set;
public string name get; set;
public bar bar get; set;
Adding objects from another object
exceldata = ParseDocument.Parse(filestream);
foo fooexample = new foo();
foreach (var object in exceldata)
fooexample.id = object.id;
fooexample.bar = new bar();
fooexample.bar.id = object.bar.id;
There is a shorthand in C# to instantiate an object and set it's properties on one line so you could alternatively have
exceldata = ParseDocument.Parse(filestream);
foo fooexample = new foo();
foreach (var object in exceldata)
fooexample.id = object.id;
fooexample.bar = new bar(id = object.bar.id);
Or using a collection
Class foo
public class foo
public int id get; set;
public string name get; set;
public ICollection<bar> bar get; set;
Adding objects from another object
exceldata = ParseDocument.Parse(filestream);
foo fooexample = new foo();
fooexample.bar = new List<bar>();
foreach (var object in exceldata)
fooexample.id = object.id;
fooexample.bar.Add(new bar(id = object.bar.id));
As others have noted you could alternatively initialize the List in the 'foo' class.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I need the class bar.id from class foo, so it would be like this in foreach --> fooexample.bar.id = object.bar.id
– Christian Quirante
Aug 10 at 14:42