Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring 2-dimensional ints as Readonly/const in separate class whilst keeping non-exposed
    text
    copied!<p>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...</p> <p>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 <code>private static readonly</code> arrays that I expose through the use of wrapping in private <code>ILists</code> 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:</p> <pre><code>namespace MyNamespace { public static partial class Tables { private static readonly string[] _Messages = { "One", "Two" }; private static readonly IList&lt;string&gt; MessagesReadOnly = Array.AsReadOnly(_Messages); public static IList&lt;string&gt; Messages { get { return MessagesReadOnly; } } } } </code></pre> <p>That much I understand but now I've got a 2 dimensional table of data (<code>int</code>) that I need to store in a similarly non-exposed manner that I can access.<br> 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.</p> <p>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 <code>Array.AsReadOnly(myArray)</code> 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 <strong>jagged</strong> 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?</p> <p>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 <code>main</code> 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?</p> <p>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.</p> <p>Let me know if you need more info about what I've been trying and thanks for reading.</p> <p>I've just been adding 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...?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload