Call method in partial class

二次信任 提交于 2019-12-11 12:29:58

问题


I'm using Visual Studios. I wrote a method in a form1.cs file in a partial class

private void TestMethod1()
{
}

I want to call this method in form2.designer.cs, in the same partial class. I tried this:

TestMethod1(); 

but I got the error method not found.

this is the form.cs namespace classA { public partial class A : B {....
private void TestMethod1() { } } }

this is the form.designer.cs namespace classA { partial class A { private void InitializaCOmponent() { ..... } (where I call my function) TestMethod1(); } }


回答1:


If the situation is as you described, then the compiler should not generate the error message as it is valid code.

However, if you try to use the visual editor, and you insert the call in your code inside the InitializeComponent method you will get an error.

This is caused by the Form editor not being able to call functions that are defined within the class you are actually editing - it is a bit restrictive about what you can do within that scope.



来源:https://stackoverflow.com/questions/34912168/call-method-in-partial-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!