[英]How do I call a function defined in a C++ DLL that has a parameter of type int *, from inside C# code?
[英]Call a function that has parameter type inside another function c#
我在表单上得到了这个功能:
private void UpdateQuantityDataGridView(object sender, DataGridViewCellEventArgs e)
{
(...codes)
}
我想在另一个函数中调用该函数,假设当我单击“确定”按钮时,此下面的函数将运行并执行具有参数类型的上面的函数。
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")
}
我知道当我们放置如下代码时,此函数将运行: dataGridView1.CellValueChanged += new DataGridViewSystemEventHandler(...);
但是,我不希望这样,因为只有在更改了DataGridView中的单元格值时,该函数才会运行,我想在单击“确定”按钮时访问该函数。 但是,我应该在参数值中放入什么?
提取当前在UpdateQuantityDataGridView()
方法中的逻辑,并将其放入名为所需名称的新public
方法中,然后可以从类中的任何位置或引用该类的任何其他代码中调用此逻辑,如下所示:
public void DoUpdateQuantityLogic()
{
// Put logic here
}
注意:如果您实际上并未使用sender
或e
,则可以使上面的方法不带参数,但是例如,如果您确实使用e
,则需要为DoUpdateQuantityLogic()
方法使用一个参数来说明您正在使用的e
对象的属性是。
现在,您可以从其他方法中调用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();
}
如果您选择对该逻辑进行单元测试,这将使您可以重用逻辑并将功能隔离到使单元测试更容易的方法中。
如果确定要使用现有的基于事件的方法基础结构,则可以为事件处理程序的sender
和e
参数传递null
,如下所示:
UpdateQuantityDataGridView(null, null);
如果您的方法UpdateQuantityDataGridView()
实际使用参数sender
和e
? 如果不是,则为两者都传递null。
UpdateQuantityDataGridView(null, null);
如果您正在使用它们:
var e = new DataGridViewCellEventArgs();
// assign any properties
UpdateQuantityDataGridView(dataGridView1, e);
您可以使用sender ,但不能使用e,因为UpdateQuantityDataGridView需要e的类型为DataGridViewCellEventArgs 。
根据您的UpdateQuantityDataGridView处理程序想要对e参数进行的操作,当您从SubmitButton调用null时,可以只传递null 。 否则,您将必须新建一个DataGridViewCellEventArgs并使用您自己的处理程序需要/期望的适当值填充它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.