C# 为什么我不能在另一个类中调用公共方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/330138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I call a public method in another class?
提问by Harris
I have got these two classes interacting and I am trying to call four different classes from class one for use in class two.
我让这两个类进行交互,并且我试图从第一类中调用四个不同的类以用于第二类。
The methods are public and they do return values but for some reason there is not a connection being made.
The error I get when I try is: "An object reference is required for the nonstatic field, method, or property 'GradeBook.[method I want called]'"
这些方法是公共的,它们确实返回值,但由于某种原因没有建立连接。我尝试时遇到的错误是:"An object reference is required for the nonstatic field, method, or property 'GradeBook.[method I want called]'"
I have everything initialized. I don't want to create the methods as static. I read over the specifics of my assignment again and I'm not even supposed to but I can't seem to get this to work anyway I word it.
我已经初始化了一切。我不想创建静态方法。我再次阅读了我的作业的细节,我什至不应该这样做,但无论如何我都无法让它发挥作用。
myGradeBook.[method] GraceBook.[method]
myGradeBook.[方法] GraceBook.[方法]
It all seems to create errors.
这一切似乎都会造成错误。
The current errors:
目前的错误:
The best overloaded method match or 'System.Console.WriteLine(string, object)' has some invalid arguments.
Arugment '2': cannot convert from 'method group' to 'object'
I'm not even sur what those mean....
我什至不明白那些是什么意思......
EDIT: I just fixed that problem thanks to the Step Into feature of Visual Studio. I don't know why it took me so long to use it.
编辑:由于 Visual Studio 的 Step Into 功能,我刚刚解决了这个问题。我不知道为什么我花了这么长时间才使用它。
回答by Jeffrey L Whitledge
You have to create a variable of the type of the class, and set it equal to a new instance of the object first.
您必须创建一个类类型的变量,并首先将其设置为等于对象的新实例。
GradeBook myGradeBook = new GradeBook();
Then call the method on the obect you just created.
然后在您刚刚创建的对象上调用该方法。
myGradeBook.[method you want called]
回答by JaredPar
You're trying to call an instance method on the class. To call an instance method on a class you must create an instance on which to call the method. If you want to call the method on non-instances add the static keyword. For example
您正在尝试调用该类的实例方法。要在类上调用实例方法,您必须创建一个实例来调用该方法。如果要在非实例上调用该方法,请添加 static 关键字。例如
class Example {
public static string NonInstanceMethod() {
return "static";
}
public string InstanceMethod() {
return "non-static";
}
}
static void SomeMethod() {
Console.WriteLine(Example.NonInstanceMethod());
Console.WriteLine(Example.InstanceMethod()); // Does not compile
Example v1 = new Example();
Console.WriteLine(v1.InstanceMethod());
}
回答by discorax
It sounds like you're not instantiating your class. That's the primary reason I get the "an object reference is required" error.
听起来你没有实例化你的类。这是我收到“需要对象引用”错误的主要原因。
MyClass myClass = new MyClass();
once you've added that line you can then call your method
添加该行后,您可以调用您的方法
myClass.myMethod();
Also, are all of your classes in the same namespace? When I was first learning c# this was a common tripping point for me.
另外,您的所有类都在同一个命名空间中吗?当我第一次学习 c# 时,这对我来说是一个常见的绊脚石。
回答by discorax
For example 1 and 2 you need to create static methods:
例如 1 和 2 您需要创建静态方法:
public static string InstanceMethod() {return "Hello World";}
Then for example 3 you need an instance of your object to invoke the method:
然后,例如 3,您需要一个对象实例来调用该方法:
object o = new object();
string s = o.InstanceMethod();