locked
How to declare an array of arrays of struct? RRS feed

  • Question

  • I am trying to build a cluster database that consists of 64 box records, and a master database that consists of 16 clusters (records). In my mind this is a two dimensional array of [64][16]. I have tried declaring these structures as shown below, but C# complains about the declaration of the master database trying to use the cluster database as a type, when it is actually a field. Can someone suggest a better way to define these structures?

    public struct BoxRecord

    {

    public bool BoxEnabled;

    public string BoxNo;

    public string CustomerName;

    public string Contact1;

    public string Contact2;

    public bool SendMsgArrival;

    public bool SendMsgRetreival;

    public byte SensorRow;

    public byte SensorCol;

    }

    public static BoxRecord[] ClusterDatabase = new BoxRecord[64];

    and finally

    public static ClusterDatabase[] MasterDatabase = new ClusterDatabase[16];

     

    Sunday, November 5, 2006 2:13 PM

All replies

  • try this:

    public struct BoxRecord

    {

    public bool BoxEnabled;

    public string BoxNo;

    public string CustomerName;

    public string Contact1;

    public string Contact2;

    public bool SendMsgArrival;

    public bool SendMsgRetreival;

    public byte SensorRow;

    public byte SensorCol;

    ClusterDatabase[] MasterDatabase;

    }

    And then use it like this:
    BoxRecord[] ClusterDatabase = new BoxRecord[64];
    foreach(
    BoxRecord br in ClusterDatabase )
    {
        br.
    MasterDatabase = new ClusterDatabase[16];
        ...// custom actions
        ...
    }

    Sunday, November 5, 2006 3:42 PM
  • Hmmmm, You are taking it wrong i think. there are other ways to do this!

    Other way to do this is using Generics is like:

    public static BoxRecord[] ClusterDatabase = new BoxRecord[64];

    List<BoxRecord[] > lst = new List<BoxRecord[] >();

    Now you can add BorxRecord[] to Generic List lst.Add() method.

    Other ways is:

    public static BoxRecord[,] ClusterDatabase = new BoxRecord[16, 64];

    I hope this is quite what you want to achieve. Right?

    Best regards,

    Monday, November 6, 2006 1:23 PM
  • public static BoxRecord[] ClusterDatabase = new BoxRecord[64];

    public static ClusterDatabase[] MasterDatabase = new ClusterDatabase[16];

    The fist line creates a new variable named ClusterDatabase.  The second line requires a type named ClusterDatabase.

    One way to handle this is to create a actually class call ClusterDatabase.



    class ClusterDatabase
    {
          BoxRecord[] innerArray
          public ClusterDatabase()
          {
                 innerarray = new BoxRecord[65];
          }

         /// Added indexer redirecting to innerArray
         BoxRecord this(int inx)
         {
               get { return innerArray[inx]; }
               set { innerArray[inx] = value;}
         }
    }

     

    Then the list

    ClusterDatabase[] MasterDatabase = new ClusterDatabase[16];

    should work just fine.

     

    Monday, November 6, 2006 3:46 PM