Introduction
I found I had a need to store simple arrays in properties for easy retrieval throughout an application such that they may be set in form A, displayed in form B or report A, further changed by form C and then viewed again in form A or form B or report A or report B. This is not the only solution to this problem (indexers, for example) but it suited me and, what I think makes it interesting, is that I could not find any documentation describing this method of using a property and an array anywhere.
Using the code
The code can be wrapped into a class similar to:
using System;
namespace MyNamespace {
public class MyClass {
public MyClass() {}
public const int Counter = 10;
private static int[] _widget = new int[Counter];
public static int [] Widget {
get { return _widget; }
set { _widget = value; }
}
}
}
The property can be populated as follows...
for (int <code>i = 0; i < MyClass.Counter; i++) {
MyClass.Widget[i] = i;
}
... and retrieve and use the array as follows:
double <code>_newWidget3 = MyClass.Widget[3];
double _newWidget5 = MyClass.Widget[5];
Points of interest
The property handles all of the indexing with no further intervention such that any array item inserted at point x can always be retrieved by referring to point x in the call. It also appears comfortable with a variety of data types such as int
, string
, object
. Though I haven't tried every type, I see no reason for it not to work with other data types.
Widget properties are defined as static
types. This is key to leveraging the power of using a property array across a variety of different objects (forms, reports, etc.) having differing scopes as well as having the ability to alter the values throughout the lifetime of the application with minimal effort.
I am amazed that I wasn't able to find examples of this since it is so simple. If you have seen other examples, please let me know. I spent some time researching this, however, that doesn't mean I didn't miss something (as we all do) so please set me straight (politely!).