[ACCEPTED]-C# accesing non static member in a static function-member
You need to tell the system which list of names 16 you're interested in. It's part of the state 15 of an object, an instance of the class... but 14 which one? Maybe you've created several instances 13 of the class - maybe you've created no instances 12 of the class. The static method has no visibility 11 of that - so which instance do you want 10 it to fetch the names
variable value from?
To 9 put it in another example, suppose we had 8 a class like this:
public class Person
{
public double MassInGrams { get; set; }
public double HeightInMetres { get; set; }
public static double ComputeBodyMassIndex()
{
// Which person are we interested in?
}
}
Person p1 = new Person { MassInGrams = 76203, HeightInMetres = 1.8 };
Person p2 = new Person { MassInGrams = 65000, HeightInMetres = 1.7 };
double bmi = Person.ComputeBodyMassIndex();
What would you expect 7 the result to be? You've asked the Person
class 6 to compute "the BMI" but without telling 5 it whose BMI to compute. You need to give it 4 that information.
Some options for your situation:
- Change
names
to be static instead - Change the method to be an instance method
- Pass in an instance of the class
- Create an instance of the class, possibly returning it
- Fetch an instance of the class some other way
By 3 the way, that's a very strange method name 2 for something which adds a name. It's also somewhat 1 unconventional...
You need to make names
static if you want to 4 use it from inside of a static method:
// If this is static, you can use it from your static method
static List<string> names = new List<string>();
The 3 issue is that getName
is defined on your type, not on an instance 2 of the type. However, names
is defined so each 1 instance of your type gets its own value.
names
is an object that will exist in the instances 13 of the class e.g. MyClass mc = new MyClass();
then you can access mc.names
. A 12 static field can be called without an instance 11 of the class just with the classname, e.g. MyClass.getName("");
will 10 work. So when you think logically, the class 9 doesn't contain names, only 'the instances 8 of that class' contain it. Therefore, you 7 either make that list static too and it 6 will be 'the same List instance' everywhere 5 when you call MyClass.names
or make the getName method 4 non-static, and it will be only called from 3 instances, therefore no MyClass.getName("")
will be possible, but 2 mc.getName("");
It's a matter of what you are exactly trying 1 to do.
Static methods can not access class fields. Either 4 make names static, or make getName() non-static. What 3 do you mean by "Compatible". Ask yourself... does 2 the method need to be static? What is its 1 purpose and how do you intend to use it?
You cant access it this way, you need to 1 instanciate the class containing a member.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.