0

I am trying to create an struct-array. I used the example provided by @JonSkeet found in this SO post to create the example below:

 public class JonSkeetClass
    {

        public struct ReportDetails
        {
           private readonly int indexd;
           private readonly string name;

            public ReportDetails(int indexd, string name)
            {
                this.indexd = indexd;
                this.name = name;
            }

            public int Indexd { get { return indexd; } }
            public string Name { get { return name; } }
        }


        static readonly IList<ReportDetails> MyArray = new ReadOnlyCollection<ReportDetails>
        (
        new[]
        {

            new ReportDetails(0, "Daily Unload Counts by Group"),
                    new ReportDetails(1,"Daily Unloads")        

        });


      public statis IList<ReportDetails> GetMyArray
      {
        get{ return MyArray;  }
       }


    }

I am now unsure how to use this class in my code as the MyArray IList is not exposing any methods or properties.

Update1: Code sample above has been updated with the suggestion from @Adrian.

To initialize:

IList<JonSkeetClass.ReportDetails> MyArray = JonSkeetClass.GetMyArray;
MessageBox.Show( MyArray[0].Name.ToString());
Community
  • 1
  • 1
John M
  • 14,338
  • 29
  • 91
  • 143

1 Answers1

2

You need to expose it via a public method

/*public*/ class JonSkeetClass  /*the visibility of this class depends on where you'll be using it*/
    {
         public struct ReportDetails /*this needs to be public also (or internal)*/
         {
             ....
         }
         public static  IList<ReportDetails> GetMyArray
         {
             get
             {
                return MyArray;
             }
         }

    }

edit

You can't access MyArray field outside the class because is a private member. This means you need to add a public property which exposes this field.

To access MyArray[0].Name, call

JonSkeetClass.GetMyArray[0].Name

edit 2

Actually you don't need an extra property, since the collection is readonly, also the items, make that field public, that's it

public static readonly IList<ReportDetails> MyArray ...
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • Thanks. That makes the class visible within the program but how do I now call the values? I was thinking it would be MyArray[0].Name. – John M Mar 26 '12 at 20:00