35159

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

Question:

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.<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.

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 <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?

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 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...?

Answer1:

<blockquote>

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

</blockquote>

If you're going to be storing this in a custom class, you could just write a <a href="http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx" rel="nofollow">custom read-only indexer property</a> 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]; } {

Answer2:

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.

Answer3:

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(); }

Recommend

  • ImageView size for phones and tablets
  • How to make a reader app where text should adjust in multiple pages according to screen size and fon
  • Relative dimensions issue with android:supportsRtl=“false”
  • Unsupported type 'add-resource' in Android Studio
  • Does using package generics require the package to be in Depends or Imports?
  • Extract all words between two phrases using regex [duplicate]
  • Obtain access token for both Microsoft Graph and individual service API endpoints (Outlook REST APIs
  • How to make a div appear under button
  • QObject::findChild() returns None without obvious reason
  • Spring Integration debounce/deduplicate
  • If I am creating a simple client server application in IntelliJ, how should this work?
  • Cuda Clang and OS X Mavericks
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • GridView breaks while scrolling
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Is there a way to do normal logging with EureakLog?
  • copying resource to sdcard gives a damaged file in android
  • Database structure design with variable amounts of fields
  • how to adjust image in a panel in Java swing?
  • Using jQuery closest() method with class selector
  • How do I change content of ComboFieldEditor?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Array.prototype.includes - not transformed with babel
  • Problems to linebreak with an int in JLabel
  • How reduce the height of an mschart by breaking up the y-axis
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • SQL merge duplicate rows and join values that are different
  • Benchmarking RAM performance - UWP and C#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal