局部类中的调用方法 [英] Call method in partial class

查看:59
本文介绍了局部类中的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio。我在局部类的form1.cs文件中编写了一个方法

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

private void TestMethod1()
{
}

我想在form2.designer.cs中的同一局部类中调用此方法。我尝试过:

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.

这是form.cs
命名空间类A
{
公共部分类A:B
{... 。

private void TestMethod1()
{
}
}
}

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

form.designer.cs
命名空间类A
{
部分类A
{
private void InitializaCOmponent()
{
... ..
}
(我称之为函数)
TestMethod1();
}
}

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

推荐答案

如果情况如您所描述,则编译器不应生成错误消息,因为它是有效的代码。

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

但是,如果尝试使用可视化编辑器,则会在代码中将调用插入到中InitializeComponent 方法会出现错误。

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.

这篇关于局部类中的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆