lundi 9 juillet 2018

Define same methods differently under a condition

Let's say condition is a boolean variable. I want to know if its possible that a group of methods (Button_Click and Label_Click in this example) is defined differently everytime the condition changes.

I am aware that i can do this:

private void Button_Click()
{
    if(condition)
    {
        //do this
    } 
    else
    {
        //do that
    }
}

private void Label_Click()
{
    if(condition)
    {
        //do this thing
    } 
    else
    {
        //do that thing
    }
}

But, in order to not have to create an if statement in each of the methods and keep the code tidier, is there the option to do something along the lines of:

if(condition)
{
    private void Button_Click()
    {
        //do this
    }
    private void Label_Click()
    {
        //do this thing
    }
} 
else 
{
    private void Button_Click()
    {
        //do that
    }
    private void Label_Click()
    {
        //do that thing
    }
}

Aucun commentaire:

Enregistrer un commentaire