简体   繁体   中英

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(...);

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. 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:

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.

Now you can call DoUpdateQuantityLogic() from you other methods, like this:

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:

UpdateQuantityDataGridView(null, null);

If your method UpdateQuantityDataGridView() actually using the parameters sender and e ? If not just pass null for both.

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 .

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 . Otherwise, you'll have to new a DataGridViewCellEventArgs and populate it with the appropriate values your own handler requires/expects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM