C# 如何要求一个方法从另一个方法重复自身
我需要方法1在方法3中重复它自己。我该怎么做C# 如何要求一个方法从另一个方法重复自身,c#,C#,我需要方法1在方法3中重复它自己。我该怎么做 代码示例[edit1]: namespace NamespaceName { public partial class ClassName { private void Method1(object sender, EventArgs e) { Statement1; Statement2; Statement3; } p
代码示例[edit1]:
namespace NamespaceName
{
public partial class ClassName
{
private void Method1(object sender, EventArgs e)
{
Statement1;
Statement2;
Statement3;
}
public void Method2 (object sender, MouseEventArgs e)
{
//Another bunch of statements
}
private void Method3 (object sender, EventArgs e)
{
"Want to repeat those statements from Method1 without copying them here"
}
}
}
另外,我是编程新手,我不知道我的选择是什么,搜索网页也没什么帮助对不起,如果这个问题已经被问到了,如果是的话,你能把它链接到这里吗
Edit1:嗯,为了写一个抽象的例子而不是我的实际代码,我省略了一些重要的东西。现在它看起来应该更合适了。告诉我它是否不是你想要的 您只需在Method3中调用方法Method1
private Method3
{
//"Want to repeat those statements from Method1 without copying them here"
//For your sender parameter, assuming that was a button named btn1.
//you can send the btn1 as sender and null as eventargs.
Method1(btn1, null); //Will execute the code in your method1.
}
您可以调用其他方法:
public void Method3(){Method1();}
-我不确定这是否是您实际要求的。您的意思是要调用Method1
?比如,Method1()代码>?你被困在哪里还不清楚。也许你刚刚开始C#教程,需要继续学习?(还要注意,伪代码往往只能得到伪答案。)这有点像是,但我把我原来的代码示例搞砸了,所以它没有显示重要的部分(很抱歉)。我已经编辑了它,还尝试了你的解决方案。不幸的是,由于“sender”参数,它无法工作。