Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Referencing an Array from another Class

Referencing an Array from another Class

Scheduled Pinned Locked Moved C#
questiondata-structuresjsontutoriallearning
3 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_16029471
    wrote on last edited by
    #1

    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.

    OriginalGriffO J 2 Replies Last reply
    0
    • M Member_16029471

      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.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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!

      "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

      1 Reply Last reply
      0
      • M Member_16029471

        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.

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        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
        ...
        },
        }
        };

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups