简体   繁体   English

如何在C#中的另一个类之外调用非静态方法?

[英]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() ,编译器会认为“好, frmForm ,但是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.

相关问题 一个非静态类如何调用另一个非静态类的方法? - How can a non-static class call another non-static class's method? 如何从 C# 中的静态方法调用非静态方法? - How do I call a non-static method from a static method in C#? C ++ Interop:如何从本机C ++调用C#类,这个类是非静态的? - C++ Interop: How do I call a C# class from native C++, with the twist the class is non-static? 在另一个类/表单中调用非静态方法 - Call a non-static method in another Class/Form 如何在Visual Studio中(从另一个非静态方法)在导入的项目中调用非静态方法 - How to call a non-static method in an imported project (from another non-static method) in Visual Studio 如何从其他类的静态方法中调用非静态方法? - How do I call a non-static method from a static method from a different class? 在 C# 中调用具有方法属性的非静态方法 - Call non-static methods with method attributes in C# C#可能在当前类中调用受保护的非静态方法的方法吗? - C# Possible method to call protected non-static methods in current class? 非静态类工厂方法-C# - non-static class factory method - C# 静态类的静态方法与非静态类的静态方法(C#) - Static Method of a Static Class vs. Static Method of a Non-Static Class ( C# )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM