[英]Call a function that has parameter type inside another function c#
i got this function on my form: 我在表单上得到了这个功能:
private void UpdateQuantityDataGridView(object sender, DataGridViewCellEventArgs e)
{
(...codes)
}
and i want to call that function inside another function, let's say when i click a "OK" button, this below function will run and execute above function that has parameter type. 我想在另一个函数中调用该函数,假设当我单击“确定”按钮时,此下面的函数将运行并执行具有参数类型的上面的函数。
private void button5_Click(object sender, EventArgs e) // This is the "OK" button click handler.
{
SubmitButton(sender, e);
}
private void SubmitButton(object sender, EventArgs e) // This is function of "OK" button
{
(...codes)
UpdateQuantityDataGridView("What should i put in here? I tried (sender, e), but it is useless")
}
I know that this function run when we put something like this: dataGridView1.CellValueChanged += new DataGridViewSystemEventHandler(...);
我知道当我们放置如下代码时,此函数将运行: dataGridView1.CellValueChanged += new DataGridViewSystemEventHandler(...);
But, i don't want that because that function will only run if the cell value in DataGridView has been changed, i want to access that function when i click "OK" button. 但是,我不希望这样,因为只有在更改了DataGridView中的单元格值时,该函数才会运行,我想在单击“确定”按钮时访问该函数。 But, what should i put inside a parameters value? 但是,我应该在参数值中放入什么?
Extract the logic currently in the UpdateQuantityDataGridView()
method and put it into a new public
method named whatever you want, then you can call this logic from anywhere in your class or any other code that references your class, like this: 提取当前在UpdateQuantityDataGridView()
方法中的逻辑,并将其放入名为所需名称的新public
方法中,然后可以从类中的任何位置或引用该类的任何其他代码中调用此逻辑,如下所示:
public void DoUpdateQuantityLogic()
{
// Put logic here
}
Note: If you do not actually use sender
or e
, then you can leave the method above without parameters, but if you do use e
, for example, then you need to have a parameter for the DoUpdateQuantityLogic()
method to account for what the property of the e
object you are using is. 注意:如果您实际上并未使用sender
或e
,则可以使上面的方法不带参数,但是例如,如果您确实使用e
,则需要为DoUpdateQuantityLogic()
方法使用一个参数来说明您正在使用的e
对象的属性是。
Now you can call DoUpdateQuantityLogic()
from you other methods, like this: 现在,您可以从其他方法中调用DoUpdateQuantityLogic()
,如下所示:
private void button5_Click(object sender, EventArgs e) // This is the "OK" button click handler.
{
DoUpdateQuantityLogic();
}
private void SubmitButton(object sender, EventArgs e) // This is function of "OK" button
{
DoUpdateQuantityLogic();
}
This allows you to re-use your logic and also isolates the functionality into a method that makes unit testing easier, if you choose to unit test this logic. 如果您选择对该逻辑进行单元测试,这将使您可以重用逻辑并将功能隔离到使单元测试更容易的方法中。
If you are determined to use your existing event-based method infrastructure, then you can pass null
for both the sender
and the e
arguments of the event handler, like this: 如果确定要使用现有的基于事件的方法基础结构,则可以为事件处理程序的sender
和e
参数传递null
,如下所示:
UpdateQuantityDataGridView(null, null);
If your method UpdateQuantityDataGridView()
actually using the parameters sender
and e
? 如果您的方法UpdateQuantityDataGridView()
实际使用参数sender
和e
? If not just pass null for both. 如果不是,则为两者都传递null。
UpdateQuantityDataGridView(null, null);
If you are using them: 如果您正在使用它们:
var e = new DataGridViewCellEventArgs();
// assign any properties
UpdateQuantityDataGridView(dataGridView1, e);
You can use sender , but you can't use e because UpdateQuantityDataGridView needs e to be of type DataGridViewCellEventArgs . 您可以使用sender ,但不能使用e,因为UpdateQuantityDataGridView需要e的类型为DataGridViewCellEventArgs 。
Depending on what your UpdateQuantityDataGridView handler wants to do with the e parameter, you could just pass null when you call it from your SubmitButton . 根据您的UpdateQuantityDataGridView处理程序想要对e参数进行的操作,当您从SubmitButton调用null时,可以只传递null 。 Otherwise, you'll have to new a DataGridViewCellEventArgs and populate it with the appropriate values your own handler requires/expects. 否则,您将必须新建一个DataGridViewCellEventArgs并使用您自己的处理程序需要/期望的适当值填充它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.