调用实用程序类中的函数
我在实用程序类中有一个函数,
namespace GUI.code
{
public class Utility
{
public string GetFileName(string grpID)
{
string filenameUNC = "\\\\" + "localhost" + "\\AgentShare\\";
string realPath = GetPath(filenameUNC);
return realPath;
}
}
}
现在我从项目中的另一个页面调用这个函数,如下所示:
new utility.GetCSFileName(ID);
为什么我需要添加新的,为什么我不能像
GetCSFileName(ID);
时
using GUI.code;
在顶部
那样调用它,如果我删除新的我会收到此错误
错误 1 非静态字段、方法或属性“copiunGUI.code.GUIUtility.GetCSFileName(string)”需要对象引用
任何建议
I have a function in a utility class
namespace GUI.code
{
public class Utility
{
public string GetFileName(string grpID)
{
string filenameUNC = "\\\\" + "localhost" + "\\AgentShare\\";
string realPath = GetPath(filenameUNC);
return realPath;
}
}
}
now i call this function from another page in the project like this:
new utility.GetCSFileName(ID);
why do i need to add new, why cant i call it like
GetCSFileName(ID);
when i have
using GUI.code;
on top
if i remove new i get this error
Error 1 An object reference is required for the non-static field, method, or property 'copiunGUI.code.GUIUtility.GetCSFileName(string)
any suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果将声明更改为
public static string GetFileName(string grpID)
,则可以调用Utility.GetFileName()
,static
表示该成员是共享的在Utility
类的所有实例中。请记住,C# 是一种面向对象语言。非静态成员称为实例成员,并且必须在类的不同实例上调用。每次调用new Utility()
时,您都在创建这样一个实例,这与其他实例不同。当类的每个实例需要在内部维护信息(状态)时,这非常有用。但是,如果某个特定成员不使用状态数据(它接受参数、工作并可选择返回结果),则可以将其声明为静态。然后,您不是从实例变量而是从类名本身调用它(在本例中为
Utility.GetFileName()
)。当类的所有成员都是
static
时,您可以将static
添加到类声明本身中,此时您将永远无法调用new Utility()
,这可能是您在本例中想要的。You can call
Utility.GetFileName()
if you change the declaration topublic static string GetFileName(string grpID)
static
means that the member is shared among all instances of theUtility
class. Remember, c# is an object-oriented language. Non-static members are called instance members, and must be called on a distinct instance of the class. Each time you callnew Utility()
, you are creating such an instance, and this is distinct from every other.This is useful when each instance of a class needs to maintain information (state) internally. If, however, a particular member does not use state data -- it accepts parameters, does work, and optionally returns a result -- it can be declared static. Then you call it not from the instance variable but from the class name itself (in this case
Utility.GetFileName()
.When all of the members of a class are
static
, you can addstatic
to the class declaration itself. At that point, you would never be able to callnew Utility()
, which might be what you want in this case.您需要将您的方法标记为静态。
然后,您将能够使用
Utility.GetFileName(...)
调用您的方法阅读有关静态方法/类的更多信息 此处。
You need to mark your method as static.
You will then be able to call your method with
Utility.GetFileName(...)
Read more on static methods / classes here.
看起来您想将您的方法(也可能是您的类)标记为静态:
然后您可以这样调用它:
It looks like you want to mark your method (and possibly your class also) as static:
Then you can call it like this:
使方法静态,您可以在没有类实例的情况下使用它,
例如
用法:
Make the method static and you can use it without an instance of the class
eg
usage:
如果您将方法设置为静态方法,则不必拥有新的方法:
那么您仍然需要调用类名称,但不必实例化它:
所以而不是:
实用程序 util = new 实用程序();
util.GetFileName("myString");
你可以这样做:
而你不能只做
GetFileName("myString")
的原因是你没有从定义它的类内部调用它。You don't have to have a new one, if you set up your method as a static method:
Then you still have to call the Class name, but you don't have to instantiate it:
so instead of:
Utility util = new Utility();
util.GetFileName("myString");
you can do:
And the reason you can't do just
GetFileName("myString")
is that you're not calling it from inside the class where it's defined.当一个方法用
static
关键字标记时,这意味着您不必按照您的意愿创建对象的新实例(使用“new”)来调用该方法。需要注意的一件事是,如果将方法标记为静态方法,则它不能调用任何非静态方法,只能调用静态方法。您也不能使用对象的任何非静态属性。
When a method is marked with the
static
keyword it means that you don't have to create a new instance of the object (using 'new') to call the method, as you intend.One thing to watch out for is that if you mark a method as being static, it cannot call any non-static methods, only static ones. You also can't use any properties of the object that are non-static.
诀窍是将方法定义为静态。这样就可以了:
现在您可以编写
utility.GetCSFileName(ID);
。但还是不得不提一下阶级。
the trick is to define method as static. This will do it:
now you can write
utility.GetCSFileName(ID);
.But you still have to mention class.