Referencing an Array from another Class
-
Hi, I have the following get / set class,...
internal class PostSections { public string title { get; set; } public string\[\] highlight { get; set; } public string\[\] highlight2 { get; set; } public bool highlightFirstWord { get; set; } public bool sendToValidation { get; set; } public string startDate { get; set; } public string endDate { get; set; } public Sectionquery\[\] sectionQueries { get; set; } } public class Sectionquery { public string id { get; set; } public string name { get; set; } public string\[\] keywords { get; set; } public bool active { get; set; } }
I can set the values for 'title', 'highlight' etc., but I cannot work out how to reference and set the values for items within the 'Sectionquery' class, eg: id, name, keywords and active. Here's my code,...
var newPost = new PostSections() { title = "Title of Book", highlight = list.ToArray(), highlight2 = list.ToArray(), highlightFirstWord = false, sendToValidation = false, startDate = "2022-11-22T00:00:00.000Z", endDate = "2022-11-22T00:00:00.000Z", sectionQueries = new Sectionquery\[\] { } };
Using the above, my API call works, however I need to set values within that Sectionquery before calling the API. How to I reference them? Hope my question's clear, thanks in advance.
-
Hi, I have the following get / set class,...
internal class PostSections { public string title { get; set; } public string\[\] highlight { get; set; } public string\[\] highlight2 { get; set; } public bool highlightFirstWord { get; set; } public bool sendToValidation { get; set; } public string startDate { get; set; } public string endDate { get; set; } public Sectionquery\[\] sectionQueries { get; set; } } public class Sectionquery { public string id { get; set; } public string name { get; set; } public string\[\] keywords { get; set; } public bool active { get; set; } }
I can set the values for 'title', 'highlight' etc., but I cannot work out how to reference and set the values for items within the 'Sectionquery' class, eg: id, name, keywords and active. Here's my code,...
var newPost = new PostSections() { title = "Title of Book", highlight = list.ToArray(), highlight2 = list.ToArray(), highlightFirstWord = false, sendToValidation = false, startDate = "2022-11-22T00:00:00.000Z", endDate = "2022-11-22T00:00:00.000Z", sectionQueries = new Sectionquery\[\] { } };
Using the above, my API call works, however I need to set values within that Sectionquery before calling the API. How to I reference them? Hope my question's clear, thanks in advance.
Your array creating won't work - it creates an empty array (no elements) and an array can't be expanded or contracted. Try doing the Sectionquery creating outside the PostSections instance creation: otherwise it gets confusing. The way I'd do it is to replace the array with a list to be more flexible:
internal class PostSections
{
public string title { get; set; }
public string[] highlight { get; set; }
public string[] highlight2 { get; set; }
public bool highlightFirstWord { get; set; }
public bool sendToValidation { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }public List sectionQueries { get; set; }
}
That way, you can start by declaring the collection:
var queries = new List()
And add your new items to it (even from a JSON / XML / CSV file or a database) without knowing in advance how many elements it needs. Then just add your new items to it:
queries.Add(new SectionQuery() { ... });
queries.Add(new SectionQuery() { ... });
...And then change your PostSection instance creation to suit:
var newPost = new PostSections()
{
title = "Title of Book",
highlight = list.ToArray(),
highlight2 = list.ToArray(),
highlightFirstWord = false,
sendToValidation = false,
startDate = "2022-11-22T00:00:00.000Z",
endDate = "2022-11-22T00:00:00.000Z",sectionQueries = queries };
Make sense?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi, I have the following get / set class,...
internal class PostSections { public string title { get; set; } public string\[\] highlight { get; set; } public string\[\] highlight2 { get; set; } public bool highlightFirstWord { get; set; } public bool sendToValidation { get; set; } public string startDate { get; set; } public string endDate { get; set; } public Sectionquery\[\] sectionQueries { get; set; } } public class Sectionquery { public string id { get; set; } public string name { get; set; } public string\[\] keywords { get; set; } public bool active { get; set; } }
I can set the values for 'title', 'highlight' etc., but I cannot work out how to reference and set the values for items within the 'Sectionquery' class, eg: id, name, keywords and active. Here's my code,...
var newPost = new PostSections() { title = "Title of Book", highlight = list.ToArray(), highlight2 = list.ToArray(), highlightFirstWord = false, sendToValidation = false, startDate = "2022-11-22T00:00:00.000Z", endDate = "2022-11-22T00:00:00.000Z", sectionQueries = new Sectionquery\[\] { } };
Using the above, my API call works, however I need to set values within that Sectionquery before calling the API. How to I reference them? Hope my question's clear, thanks in advance.
Other answer is probably better but if you need to use an existing structure... You can create it dynamically (pseudo code)
int count = ....;
var sections = new Sectionquery[count];
for (int i=0; i < count; i++)
{
sections[i].id = ...
sections[i].name = ...
}
newPost.sectionQueries = sections ;You can also create it statically using the same layout you have already used. Although typically that would not be useful for the the structure you gave. But it wouldn't be used for PostSections either. Pseudo code (google for 'c# create array statically')
var newPost = new PostSections()
{
...
sectionQueries = new Sectionquery[]
{
new {
"1", // id
"xxx", // name
...
},
new {
"2", // id
"yyy", // name
...
},
}
};