简体   繁体   English

局部类中的调用方法

[英]Call method in partial class

I'm using Visual Studios. 我正在使用Visual Studios。 I wrote a method in a form1.cs file in a partial class 我在部分类的form1.cs文件中编写了一个方法

private void TestMethod1()
{
}

I want to call this method in form2.designer.cs, in the same partial class. 我想在同一部分类的form2.designer.cs中调用此方法。 I tried this: 我尝试了这个:

TestMethod1(); 

but I got the error method not found. 但是我找不到错误方法。

this is the form.cs namespace classA { public partial class A : B {.... 这是form.cs命名空间classA {公共局部类A:B {....
private void TestMethod1() { } } } 私有无效TestMethod1(){}}}

this is the form.designer.cs namespace classA { partial class A { private void InitializaCOmponent() { ..... } (where I call my function) TestMethod1(); 这是form.designer.cs命名空间。classA {部分类A {私有void InitializaCOmponent(){.....}(在这里我称为函数)TestMethod1(); } } }}

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. 但是,如果尝试使用可视化编辑器,并将调用插入到InitializeComponent方法内的代码中,则会出现错误。

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. 这是由于表单编辑器无法调用您实际上正在编辑的类中定义的函数而引起的-这对您在该范围内可以执行的操作有些限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM