Reputation: 11
I have a class where I'm supposed to have a private array.
private int[] array = new int[5];
and I'm supposed to have a public method where I put data into the array but I'm unsure of how to make it work properly.
public static void method(int a, int b)
{
`array[a-1] = b;`
}
I've tried this and it gives me an error saying an object reference is required for the non-static field, method, or property 'Class.array'. Can someone help me figure out how to deal with this problem?
Upvotes: 0
Views: 1268
Reputation: 81483
You can't access instance fields from a static method. You will need
private int[] array = new int[5];
public void method(int a, int b)
{
array[a-1] = b;
}
or debatably
private static int[] array = new int[5];
public static void method(int a, int b)
{
array[a-1] = b;
}
or you could use an indexer
private int[] array = new int[5];
public int this[int i]
{
get { return array[i]; }
set { array[i] = value; }
}
Additional Resources
Static Classes and Static Class Members (C# Programming Guide)
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
Static Members
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
Upvotes: 3
Reputation: 2875
Static methods or variables belong to the class itself (rather than a specific instance of the class). Every instance of your class will have its own array. That static method has no way of knowing what instance to use to access the array. If you need to access instance (non-static) variables, you need to specify an instance of the class.
If you don't want to create an instance of your class, you could make the array static:
private static int[] array = new int[5];
public static void method(int a, int b)
{
array[a - 1] = b;
}
If you don't want to make the array static, you'll have to create an instance.
You could make the method an instance method:
class MyClass
{
private int[] array = new int[5];
public static void method(int a, int b)
{
myClass.array[a - 1] = b;
}
}
MyClass myClass = new MyClass();
myClass.method(1,2);
Or you could create a static variable with an instance of the class:
class MyClass
{
private static MyClass myClass = new MyClass();
private int[] array = new int[5];
public static void method(int a, int b)
{
myClass.array[a - 1] = b;
}
}
The best approach would depend on you're actually trying to do.
Upvotes: 1