I would like to declare a public array to be used across my solution.
namespace SolutionName
{
internal class Input
{
public int arraySize = Convert.ToInt16(ConfigurationManager.AppSettings.Get(("size")));
public in[] array = new double[arraySize , 1];
}
}
I get the following error:
A field initializer cannot reference the non-static field, method, or property 'SolutionName.Input.arraySize'
My goal is to allow the user to dictate the size of "array" via the web config file. Any suggestions?
Loading

PJ MartinsRank #2,548 · 548 reputationPosted May 27, 2010, 8:36 PM EDT
public class Statics
{
public static double[] Array = new double[Convert.ToInt16(ConfigurationManager.AppSettings.Get(("size")))];
}
Now you can just reference Statics.Array throughout your project.
Jaish MathewsRank #225 · 11K reputationPosted May 27, 2010, 8:50 PM EDT
You can do that. But when you initializing an arry always need to use static/hardcoded size like.. public int[] array1 = new int[10];. Here you can't use a variable as array size.
But you can use variable later after arrays initialize. Below is example.
public class Program
{
//DECLARED ARRaY WITHOUT MENTIONED SIZE
public int[] array;
static void Main(string[] args)
{
Program obj = new Program();
obj.Test();
}
public void Test()
{
int arraySize = Convert.ToInt16(ConfigurationManager.AppSettings["size"]);
//Define the size of the array from configuration file
array = new int[arraySize];
}
}
Sam HobbsRank #107 · 20.5K reputationPosted May 28, 2010, 4:14 AM EDT
Perhaps the reason your code won't compile is that the type is "Array", not "array". C# is case-sensitive. If your actual code uses Array instead of array then please post the accurate code in the future. The innacuracy might not be relevant here but inaccurate code can waste people's time.
Why use the Array type? Use a collection such as ArrayList.
Rodney Johnson78 reputationPosted May 28, 2010, 5:11 AM EDT
...
Sam, thanks for the type-o catch. That is not the issue in the solution however. I use arrays because I know how they work, for the most part.
PJ MartinsRank #2,548 · 548 reputationPosted May 28, 2010, 7:57 AM EDT
private static double[] _array = null;
public static double[] Array
{
get
{
if (_array == null)
_array = new double[Convert.ToInt16(ConfigurationManager.AppSettings.Get(("size")))];
return _array;
}
}
This will still allow you to use it globally instead of having to decare a new instance of the class each time.
Rodney Johnson78 reputationPosted May 30, 2010, 1:16 PM EDT
namespace SolutionName
{
public class SizeArrayClass
{
public int[] array;
public int arraySize = Convert.ToInt16(ConfigurationManager.AppSettings.Get(("size")));
internal void SizeMyArray()
{
array = new int[size];
}
}
}
SizeMyArray() is called within the appropriate method in another class.
I am now able to use "array" and "size" throughout the project by referencing SizeArrayClass within the appropriate class(es).
PJ, I attempted to use your method. Although the build error went away I was unable to reference the array in the other class(es) where it was needed. Probably because I was not using it correctly.
Sam, your last post looks very similar to the way a professional programmer friend of mine would do it. He quite often laughs at my "pseudo-code".
Thanks guys.
Rod