简体   繁体   English

非静态字段所需的对象引用

[英]Object reference required for a non-static field

I am getting an error "An object reference is required for the non-static field, method, or property 'Excel1.Program.GetAllTemplateNames(string, string)" I know this is pretty silly but I am quite new to C# and could do with some assistance in debugging this code. 我收到一个错误“非静态字段,方法或属性'Excel1.Program.GetAllTemplateNames(string,string)需要对象引用”,我知道这很愚蠢,但是我对C#还是很陌生,可以这样做在调试此代码方面有一些帮助。 Is it even possible to call a static function from a Main function? 甚至可以从Main函数调用静态函数吗? Am having these doubts. 有这些疑问。

Since ProcessInput is static you cannot call the instance(non-static) method GetAllTemplateNames from there without having an instance of this class ( Program ). 由于ProcessInput是静态的,因此如果没有此类的实例( Program ),则无法从那里调用instance(非静态)方法GetAllTemplateNames

So you either need to make GetAllTemplateNames also static or you need to make ProcessInput non-static. 因此,您需要使GetAllTemplateNames也为静态,或者需要使ProcessInput为非静态。 I would choose the second option since GetAllTemplateNames needs to access some instance variables which is impossible when it's static. 我将选择第二个选项,因为GetAllTemplateNames需要访问一些实例变量,而这些变量在静态时是不可能的。

So change the signature of ProcessInput in the following way(note the omited static ): 因此,可以通过以下方式更改ProcessInput的签名(请注意,省略了static ):

public void ProcessInput(String strRetVal, String strFunctionName, /*String strParamCount,*/ String strParam1, String strParam2, String strParam3, String strParam4)

now you also need to change the call of this method in main to: 现在您还需要将main中此方法的调用更改为:

var p = new Program();  // create an instance
p.ProcessInput(strRetVal, strFunctionName, /*strParamCount,*/ strParam1, strParam2, strParam3, strParam4);

MSDN: static MSDN:静态

You should make the GetAllTemplateNames method static if you want to be able to call it from other static methods without an instance of a class: 如果希望不使用类的实例而从其他静态方法调用GetAllTemplateNames方法,则应将其GetAllTemplateNames static

public static void GetAllTemplateNames(String strParam, String strRetVal)

This also means that the fields that this method uses ( templateClient and taskClient must also be static) 这也意味着此方法使用的字段( templateClienttaskClient也必须是静态的)

or another possibility is to create an instance of the containing class: 或者另一种可能性是创建包含类的实例:

new Program().GetAllTemplateNames(strParam1, strRetVal);

Change this line 更改此行

      GetAllTemplateNames(strParam1, strRetVal);

to

      new Program().GetAllTemplateNames(strParam1, strRetVal);

or make the method static. 或将方法设为静态。

The problem is occurring on the line GetAllTemplateNames(strParam1, strRetVal); 该问题发生在GetAllTemplateNames(strParam1, strRetVal); , and any other calls to GetAllTemplateNames() or ReturnAllTemplateNames() . ,以及对GetAllTemplateNames()ReturnAllTemplateNames()任何其他调用。

These methods are not static, yet you are calling them from a static method! 这些方法不是静态的,但是您是从静态方法调用它们的! You'll need to make them static, or create an instance of their containing class in order to call them from a static method like main() . 您需要将它们设为静态,或创建其包含类的实例,以便从诸如main()类的静态方法中调用它们。

The main function is static, that's why you can call ProcessInput. 主要功能是静态的,这就是为什么您可以调用ProcessInput的原因。 However, you can't call a non-static function from a static one : GetAllTemplateNames has to be a static function. 但是,不能从静态函数调用非静态函数:GetAllTemplateNames必须是静态函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM