Is it correct to call a method, and pass your null parameters?

Asked

Viewed 361 times

15

For example, I have the event of click of a button calling the method below:

 //Evento
 this.serviçoToolStripMenuItem1.Click += new System.EventHandler(this.Nivel_Serv_Click);

 //Metodo
 private void Nivel_Serv_Click(object sender, EventArgs e) //tipo 3 serviço
 {
    //faz alguma coisa....
 }

In another method, I performed the above method call so:

 Nivel_Serv_Click(null, null);

Is it right, to do this? or would it be a gambit?

3 answers

14


Yes, since there is no overload that can handle your call without arguments. But it may not be the most appropriate.

I would only question if it is what you want, in general when you receive a notification that the object had an event is likely to want to know or do something with this object and discard it seems strange, but there are cases for it.

I’d say it’s gambiarra, but the mechanism of events of C# is a gambiarra. Avoid gambiarras is not always pragmatic, there is good gambiarra.

But this method is not called by you, so it will have no null arguments. If you want to call an event method directly, then maybe it’s a bigger scam.

If you want to have a method that doesn’t need these parameters, create a method like this and call it, or use a ready one (see answer from LINQ). If you want it to run within the event method, call it within the event method.

The modern idiomatic form of C# is to avoid null as much as it can. Before they were part of the philosophy of language, now only if the domain requires it.

  • 1

    I just think your ... Yes, it should already be transparent, that is, "Yes, it is a gambiarra", only after reading the whole text I realized that. 'Cause the question is a little obscure for your answer ( Is it right, do it? or would it be a gambit?)

  • 1

    @Marconciliosouza ready, I improved

11

If you want to execute the code {faz alguma coisa} when the menu option is either clicked by the user or directly by a call from his code, the "most correct" is to create a method with that code:

//Evento
 this.serviçoToolStripMenuItem1.Click += new System.EventHandler(this.Nivel_Serv_Click);

 //Método
 private void Nivel_Serv_Click(object sender, EventArgs e) //tipo 3 serviço
 {
    FazAlgumaCoisa();
 }

private void FazAlgumaCoisa()
{
    //faz alguma coisa....
}

When the menu item is clicked the system calls Nivel_Serv_Click() who for her made flame FazAlgumaCoisa().

When you want to call it via code call it so:

FazAlgumaCoisa();

9

Another important point is that all controls that are buttons have a method called PerformClick, it triggers the event of click button.

Note that there is no error in calling the event method by passing the two arguments as null if you don’t use the parameters inside Nivel_Serv_Click.

The sender, since it identifies which control triggered the event. In this case, it is best to use the PerformClick().

  • 2

    Well-observed..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.