开发者

Storing 2-dimensional ints as Readonly/const in separate class whilst keeping non-exposed

This is my first question after having used this place as my "go to" for a general opinion on what works/doesn't/why and the such. So let's try this out...

With my limited experience I've been trying to get my head round the better ways of creating fixed data fields that I can refer back to throughout my program - things like end-user-viewable strings that I show repeatedly and other parameters that I'd like to keep constant and safe from change. I've kept my reused data in separate static classes and put my strings in private static readonly arrays that I expose through the use of wrapping in private ILists that have public getters that return the single string I'm after. Hopefully I've not abused terminology so far! What I've done so far:

namespace MyNamespace
{
    public static partial class Tables
    {
          private static readonly string[] _Messages =
                  {
                      "One",
                      "Two"
                  };

          private static readonly IList<string> MessagesReadOnly = Array.AsReadOnly(_Messages);
          public static IList<string> Messages { get { return MessagesReadOnly; } }
    }
}

That much I understand but now I've got a 2 dimensional table of data (int) that I need to store in a similarly non-exposed manner that I can access.

This data is going into its own class as now I'm dealing with a separate real-world data type. Real-world scenario - I've got different fuels with their associated combustion constants that I'll need to access to perform combustion analysis using data that I get from a gas analyser. So I want to keep these arrays/lists separate with all their methods but I can't seem to figure out how to do this.

If I do like I did before and go via the route of array (but 2D this time) then I run into the issue that it seems (according to info on MSDN) that Array.AsReadOnly(myArray) only works for one-dimensional arrays. I'm not savvy enough to know exactly how to write my own to work around this (assuming that's a simple enough task). So then if I go to trying to use jagged arrays, as that seems to be the other viable route I've found, I get stuck trying to figure out how and where to create the initial jagged array (constructor or as class method) and then what about where to initialise the array within that?

If I didn't need to protect the array then I can put it all in the constructor and that might be ok (as far as I can tell so far) but I have to keep it non-exposed. The first way I understood how that programatically fit within a class but trying to use jagged arrays non-exposed has got me all muddled up. All the examples I see on the web seem to create them and initialise within main which is fine (although exposed) but as I'm putting this elsewhere how can I make it available to anything that might need the data whilst not exposing them?

Hopefully you understand why I feel like I'm going in circles, maybe the answer is really simple and I'm missing the obvious but until I see someone else do similar I can't figure it out, and I haven't been able to find anything close enough to give me the clues. If there already exists a similar post on SO please point me in that direction. Like I've said, I've scoured both MSDN, SO and wandered the web in search of breadcrumbs.

Let me know if you need more info about what I've been trying and thanks for reading.

I've just been a开发者_运维知识库dding tags to this question and seen that there's an Array tag so I'm off to see if I can narrow things down some more there. Not sure if I should add that tag too, I could add Lists also...?


This data is going into its own class as now I'm dealing with a separate real-world data t

If you're going to be storing this in a custom class, you could just write a custom read-only indexer property for that class.

This would look something like:

private YourType[,] internalArray; // Create and set this up in constructor or elsewhere...


public YourType this[int row, int column]
{
     get 
     {
          return internalArray[row,column];
     }
{


Couldn't you use a List<List<Yourcustom_class>> for this purpose and define a public readonly property that has only a getter for retrieving data.


You can initialize your jagged array in the static constructor of your static class.

So

public static readonly IList<IList<int>> array;

static Tables() {
    // Init array
    // Make it read only
    List<IList<int>> ar1 = new List<IList<int>>();

    for (int i = 0; i < 10; i++)
    {
        List<int> ar2 = new List<int>();
        for (int j = 0; j < 10; j++)
        {
            ar2.Add(j);
        }
        ar1.Add(ar2.AsReadOnly());
    }

    array = ar1.AsReadOnly();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜