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
Rodney JohnsonPosted May 30, 2010, 5:16 PM
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
PJ MartinsPosted May 28, 2010, 11:57 AM
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 JohnsonPosted May 28, 2010, 9:11 AM
...
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.
Sam HobbsPosted May 28, 2010, 8:14 AM
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.
Jaish MathewsPosted May 28, 2010, 12:50 AM
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];
}
}
PJ MartinsPosted May 28, 2010, 12:36 AM
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.