![](/img/trans.png)
[英]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.