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.开发者_如何学Ccode.GUIUtility.GetCSFileName(string)
any suggestions
You can call Utility.GetFileName()
if you change the declaration to public static string GetFileName(string grpID)
static
means that the member is shared among all instances of the Utility
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 call new 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 add static
to the class declaration itself. At that point, you would never be able to call new Utility()
, which might be what you want in this case.
You need to mark your method as static.
public class Utility
{
public static string GetFileName(string grpID)
{
string filenameUNC = "\\\\" + "localhost" + "\\AgentShare\\";
string realPath = GetPath(filenameUNC);
return realPath;
}
}
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:
public static class Utility
{
public static string GetFileName(string grpID)
...
Then you can call it like this:
string filename = Utility.GetFileName(ID);
Make the method static and you can use it without an instance of the class
eg
public static string GetFileName(string grpID)
usage:
Utility.GetCSFileName(ID);
You don't have to have a new one, if you set up your method as a static method:
public static string GetFileName(string grpID)
{
//your code here
}
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:
Utility.GetFileName("myString");
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.
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.
the trick is to define method as static. This will do it:
namespace GUI.code
{
public class Utility
{
public static string GetFileName(string grpID)
{
string filenameUNC = "\\\\" + "localhost" + "\\AgentShare\\";
string realPath = GetPath(filenameUNC);
return realPath;
}
}
}
now you can write utility.GetCSFileName(ID);
.
But you still have to mention class.
精彩评论