访问其他形式的私有方法
Accessing Private method of other form
如何从一种形式访问私有方法到另一种形式?
例如,我在 Form1 中有这个方法:
表格 1:
private void Test (){}
那么如何在 Form2 中访问该方法(private void Test),以便我在 Form2 中输入的值将在方法 Test 中发送??
测试是一个 datagridview,在 form 2 中我必须输入名称和相应的值,如果我按下保存按钮,它应该会自动保存在 Form1 的 datagridview 中。
private
方法完全不能在其 class
之外访问 。如果您不在 class
中,您 不能 访问 private
方法。
针对您的情况,最简单的方法是制作 private
方法 public
public void Test (){}
或者,您必须创建一个 public
包装器方法来调用您的 private
方法:
public void TestWrapper() {
Test(); //if test is private
}
然后在你的 Form2
中,你应该有 Form1
的 instance
并像这样轻松调用方法:
//All these are inside Form2
Form1 form1 = new Form1();
//Somewhere in your code
form1.Test(); //if test is public, or
form1.TestWrapper(); //if test is private
但在所有情况下,底线是:
You cannot call private
method outside of the class
.
如果要访问其他形式的方法则不能private
。您需要将它们设为 public
才能访问其他形式的方法。
您无法从另一个 class 访问私有方法。
使其成为 public 然后您可以通过另一个 class.
访问它
如何从一种形式访问私有方法到另一种形式?
例如,我在 Form1 中有这个方法:
表格 1:
private void Test (){}
那么如何在 Form2 中访问该方法(private void Test),以便我在 Form2 中输入的值将在方法 Test 中发送??
测试是一个 datagridview,在 form 2 中我必须输入名称和相应的值,如果我按下保存按钮,它应该会自动保存在 Form1 的 datagridview 中。
private
方法完全不能在其 class
之外访问 。如果您不在 class
中,您 不能 访问 private
方法。
针对您的情况,最简单的方法是制作 private
方法 public
public void Test (){}
或者,您必须创建一个 public
包装器方法来调用您的 private
方法:
public void TestWrapper() {
Test(); //if test is private
}
然后在你的 Form2
中,你应该有 Form1
的 instance
并像这样轻松调用方法:
//All these are inside Form2
Form1 form1 = new Form1();
//Somewhere in your code
form1.Test(); //if test is public, or
form1.TestWrapper(); //if test is private
但在所有情况下,底线是:
You cannot call
private
method outside of theclass
.
如果要访问其他形式的方法则不能private
。您需要将它们设为 public
才能访问其他形式的方法。
您无法从另一个 class 访问私有方法。 使其成为 public 然后您可以通过另一个 class.
访问它