简体   繁体   English

调用另一个函数中具有参数类型的函数c#

[英]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. 注意:如果您实际上并未使用sendere ,则可以使上面的方法不带参数,但是例如,如果您确实使用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: 如果确定要使用现有的基于事件的方法基础结构,则可以为事件处理程序的sendere参数传递null ,如下所示:

UpdateQuantityDataGridView(null, null);

If your method UpdateQuantityDataGridView() actually using the parameters sender and e ? 如果您的方法UpdateQuantityDataGridView()实际使用参数sendere 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.

相关问题 如何从C#代码内部调用C ++ DLL中定义的,具有int *类型参数的函数? - How do I call a function defined in a C++ DLL that has a parameter of type int *, from inside C# code? 从C#调用具有函数参数的F#函数 - Call F# function that has a function parameter from C# 是否可以从C#调用参数类型为Dataset的C ++函数 - Is it possible to call from C# a C++ function that has parameter of type Dataset C#使用FILE *参数调用C函数 - C# call C function with FILE* parameter 从C#中的另一个函数调用一个函数 - Call a function from another function in c# 如何使用从 C# 到 Rust function 有另一个 ZC1C425268E68385D1AB5074C 作为参数? - How to use from C# a Rust function that has another function as a parameter? 将参数动态注入到C#中的函数调用中 - Dynamically injecting a parameter into a function call in c# 如何从C#调用具有void * callback和object参数的C ++ Dll中的函数 - How do I call a function in a C++ Dll from C# that has void* callback and object parameter C#:调用 DLL function,其参数数据类型为“int”,返回数据类型为“string” - C#: calling a DLL function that has 'int' as a parameter data type and 'string' as the return data type C#在另一个函数中更新出参数 - C# Update out parameter in another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM