类模块引用中的错误 [英] Error in Class Module References
本文介绍了类模块引用中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使用:
if(this.OpenConnection()== true)(我正在使用public static bool for this line使用私有bool进行OpenConnection)使用C#语言在Visual Studio中的类模块中使用
。它说
关键字这个在静态属性,静态方法或静态字段初始化程序中无效
OpenConnection本身位于同一个类模块中。
I'm not able to use:
if (this.OpenConnection() == true) (I'm using public static bool for this line and using private bool for OpenConnection)
in my Class Module in visual studio using C# language. It says
"Keyword "this" is not valid in a static property, static method, or static field initializer"
OpenConnection itself is located in the same class module.
推荐答案
此错误消息中有哪些不清楚?只有当你不明白static
是什么意思时,可能还不清楚。
请看我过去的答案:
在c#中输入类型 [ ^ ],
C#windows base这个关键词有关及其在应用程序中的用途 [ ^ ],
是什么让静态方法可以访问? [ ^ ]。
-SA
What is unclear in this error message? It could be unclear only if you don't understand whatstatic
means.
Please see my past answers:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^].
—SA
你不能打电话这个
在静态方法中因为这个
引用当前的类实例,而静态方法与实例没有关联。
有两种可能的解决方案;什么是最好的,取决于你的申请:
You cannot callthis
in a static method becausethis
refers to the current class instance, and a static method is not associated with an instance.
There are two possible solutions; what's the best, depends on your application:
- 制作
OpenConnection
静态,并致电它就像一个静态方法。 - 使调用者成为一个实例方法。
- Make
OpenConnection
static, and call it like a static method. - Make the caller an instance method.
这篇关于类模块引用中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文