[英]How can I call a non-static method out of another class in C#?
The question itself sounds a bit nooby. 问题本身听起来有点不合时宜。 To call a non static method from another class you have to use the current instance of the class.
要从另一个类调用非静态方法,您必须使用该类的当前实例。 I tried to get the instance of the class with the method i want to call to the second class.
我试图用我想调用第二个类的方法来获得该类的实例。 I think I succeeded but it is still not working yet.
我认为我成功了,但仍然无法正常工作。
This one way how I tried to bring the instance to the second class: 我尝试将实例带入第二类的这种方式:
arbeitsbearbeitung arbeitenbearbeitung = new arbeitsbearbeitung(arbeit);
arbeitenbearbeitung.Parent = this;
arbeitenbearbeitung.Show();
(out of Class 1) (属于第1类)
And this is where I used it in class 2: 这是我在第二课中使用它的地方:
Form frm = (Form)this.Parent;
frm.updateGrid();
I also tried it with different ways like passing it as parameter; 我还尝试了其他方法,例如将其作为参数传递。 same result...
同样的结果
This is updateGrid()
in class 1: 这是类1中的
updateGrid()
:
public void updateGrid()
{
klassenarbeitenTableAdapter.Fill(this.database1DataSet.Klassenarbeiten);
}
It tells me that Form does not contain a definition for updateGrid
. 它告诉我Form不包含
updateGrid
的定义。
I must be dumb but I can't find my mistake. 我一定很傻,但是我找不到我的错误。
Thinking and googling for 2 hours and still no clue. 思考和谷歌搜索了2个小时,仍然没有任何线索。
I feel dumb now. 我现在觉得很蠢。 Thank you for your help!
谢谢您的帮助!
you have to use the current instance of the class
您必须使用该类的当前实例
You have to use an instance of the class. 您必须使用该类的实例。 Not necessarily the current one.
不一定是当前的。
I tried to get the instance of the class
我试图获取该类的实例
There is not the instance (unless it's a singleton), there is an instance. 没有实例 (除非它是一个单),有一个实例。
Next, Microsoft has implemented Form
as part of the .NET framework. 接下来,Microsoft已将
Form
实施为.NET框架的一部分。 Microsoft's Form
class does not have a updateGrid()
method. Microsoft的
Form
类没有updateGrid()
方法。
If you created a form, then that form inherits from Form
and you added the method updateGrid()
. 如果创建了表单,则该表单将从
Form
继承 ,并添加了updateGrid()
方法。 So instead of casting to Form
, cast it to your class. 因此,与其强制转换为
Form
,不如将其强制转换为您的类。
So the code might read 所以代码可能会读
arbeitsbearbeitung frm = (arbeitsbearbeitung) this.Parent;
frm.updateGrid();
Note that this is not clean code, since it breaks the Liskov substitution principle . 请注意,这不是干净的代码,因为它违反了Liskov替换原则 。 But I think that's not your primary concern at the moment.
但是我认为这不是您当前主要关心的问题。
Form
does not have an updateGrid
method but your derived class does. Form
没有updateGrid
方法,但您的派生类updateGrid
。 You need to cast Parent to your class: MyForm frm = (MyForm)this.Parent; 您需要将Parent强制转换为您的类:MyForm frm =(MyForm)this.Parent;
You are declaring frm
as a plain Form
: 您将
frm
声明为纯Form
:
Form frm = (Form)this.Parent;
The problem is that the Form
class has no function called updateGrid
. 问题在于
Form
类没有名为updateGrid
函数。 When you say frm.updateGrid()
, the compiler thinks "OK, frm
is a Form
, but Form
doesn't have anything called updateGrid
". 当您说
frm.updateGrid()
,编译器会认为“好, frm
是Form
,但是Form
没有什么叫做updateGrid
”。 It doesn't know that frm
will actually be your special subclass of Form
with the updateGrid
function defined. 它不知道
frm
实际上将是定义了updateGrid
函数的Form
的特殊子类。
Instead, you need to declare frm
with the actual class of Class 1: 相反,您需要使用类1的实际类声明
frm
:
MyCustomForm frm = (MyCustomForm)this.Parent;
Then when you do frm.updateGrid()
, the compiler will know what you're talking about. 然后,当您执行
frm.updateGrid()
,编译器将知道您在说什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.