[英]I cannot access the variable i declared in my class
This is my c# code 这是我的C#代码
namespace abc {
public class def: SchedulerClient
{
public string key; {}
public static void send(string abc)
{
lots of code...........
key = string;
}
}
}
I am getting an error when i try to store a string value in my variable key which i have declared as "public". 我在声明为“ public”的变量键中存储字符串值时遇到错误。
This is the error i get: 这是我得到的错误:
An object reference is required for the non-static field, method, or property 'abc .def.Dkey'
非静态字段,方法或属性'abc .def.Dkey'需要对象引用
How do i overcome this? 我该如何克服呢?
You can overcome it by making your method not static: 您可以通过使方法不是静态的来克服它:
public void send(string abc)
{
lots of code...........
key = stringValue;
}
or by making the field static: 或通过将字段设为静态:
public static string key;
At any rate, I think you need to think about your design and figure out what needs to be static and what doesn't. 无论如何,我认为您需要考虑设计并弄清楚什么是静态的,哪些不是静态的。
your method is static but your key is an instance. 您的方法是静态的,但是您的键是一个实例。 you cannot access instance properties or methods in a static reference.
您不能在静态引用中访问实例属性或方法。
either make them both static or make them both non static 将它们都设为静态或将它们都设为非静态
Static members are accessible only in static methods and static method can access static members as well as non-static members. 静态成员只能在静态方法中访问,并且静态方法可以访问静态成员以及非静态成员。 Please make the change accordingly.
请相应地进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.