Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Add new object to array in a new object

I have an object model that contains an Array of objects. The model looks like this:

public Request()
{
     RequestId = String.Empty;
     Name = String.Empty;
     Loads = Array.Empty<Load>();
}
public Load()
{
     LoadId = String.Empty;
     IsLoaded = false;
}

I need to create a new Request() object but I am having trouble figuring out the proper way to add an object to the array portion.

This is what I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var requests = new List<Request>{
new Request(){
RequestId = "1",
Name = "ATLGA",
Loads= new Load[1]{Loads[0] = new Load{IsLoaded = true} }
}

I get an error: the name "Loads’ does not exist in the current context. I have tried different syntax, but I am unable to figure this out.

>Solution :

This is how you initialize your array to one object:

Loads = new[] {  new Load { IsLoaded = true } }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading