公共类中的 C# 函数在代码隐藏中无法识别
使用 vwd express 2010 将 vb web 项目转换为 c#。 开发系统是64位Windows 7。
我在外部cs文件中声明了一个常用函数。 文件“clsCommon.cs”的内容=
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
namespace Project_Website
{
public class clsCommon
{
public void testA()
{
}
} // clsCommon
} // namespace
在后面的代码中,我尝试访问函数 testA() 如下:
testA();
也尝试过:
clsCommon.testA();
并且
Project_Website.clsCommon.testA();
我想使用的实际示例更复杂,但这是体现问题的最简单的示例( s) 我列举如下:
当我输入时,Intellisense 识别 clsCommon,但不认为 testA() 是其中的方法。 Intellisense 只看到两个方法:Equals() 和 ReferenceEquals()。
我忽略 Intellisense 并无论如何进行编译,这会产生以下错误消息:
错误 1 非静态需要对象引用字段、方法或属性“Project_Website.clsCommon.testA()”
此问题的根本原因是什么?
Converting vb web project to c# using vwd express 2010.
Development system is a 64bit windows 7.
I have a commonly used function declared in an external cs file.
Contents of file "clsCommon.cs" =
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
namespace Project_Website
{
public class clsCommon
{
public void testA()
{
}
} // clsCommon
} // namespace
In code behind I try to access the function testA() as follows:
testA();
Also tried:
clsCommon.testA();
and
Project_Website.clsCommon.testA();
The actual example I want to use is more complicated, but this is the simplest example that manifests the problem(s) I enumerate below:
The As I type, Intellisense recognizes clsCommon, but doesn't think testA() is a method in it. Intellisense only sees two methods: Equals() and ReferenceEquals().
I ignore Intellisense and compile anyway, which produces the following error message:
Error 1 An object reference is required for the non-static field, method, or property 'Project_Website.clsCommon.testA()'
What is the root cause of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要么需要使该方法静态,要么创建类的实例 clsCommon 来使用它,即:
请注意,上面的方法签名尖叫着“副作用”,因为您不这样做不返回任何内容,也不传递任何内容。如果要修改类的其他成员或属性,则应该创建一个实例并使用成员方法:
只有在以下情况下才应将该方法设置为
static
:您不需要使用以下方法访问任何其他属性或方法clsCommon
类。如果您的所有方法都是这种情况,并且 clsCommon 仅包含一堆实用方法,那么您也应该将 clsCommon 类设为静态。You either need to make that method
static
, or create an instance of the classclsCommon
to use it, i.e.:Note that above method signature screams "side effects" since you don't return anything and are not passing anything in. If you are modifying other members or properties of the class, you should create an instance instead and use a member method:
You should only make the method
static
if you do not need access to any other properties or methods with theclsCommon
class. If that is the case for all of your methods, andclsCommon
is just holding a bunch of utility methods, you should make theclsCommon
class static as well.