C # conversion from a private void to public void [closed]

0

Hello everyone, I wondered how I can convert this code to public void . Visual Studio reports an error in the and variable.

Error message: "The name 'and' does not exist in the current context"

I just made ctrl+c and ctrl+v of:

private void TextDisplay_KeyPress(object sender, KeyPressEventArgs e) 

To:

public void Detect() 

Below the body of my method:

if (e.KeyChar == 49 || e.KeyChar == 50 || e.KeyChar == 51 || e.KeyChar == 52 || e.KeyChar == 53 || e.KeyChar == 54 || e.KeyChar == 55 || e.KeyChar == 56 || e.KeyChar == 57 || e.KeyChar == 58)
        {
            TextDisplay.Text = TextDisplay.Text + e.KeyChar.ToString();
        }

        //Detetor da tecla Equal
        if (e.KeyChar == 61)
        {
            Equal();
        }

        //Detetor da tecla Multi
        if (e.KeyChar == 42)
        {
            Multi();
        }

        //Detetor da tecla Plus
        if (e.KeyChar == 43)
        {
            Plus();
        }

        //Detetor da tecla Divisão
        if (e.KeyChar == 47)
        {
            Divisão();
        }

        //Detetor da tecla  less
        if (e.KeyChar == 45)
        {
            Less();
        }

        //DetetoR da tecla backspace
        if (e.KeyChar == 8)
        {
            if (TextDisplay.Text == "")
            {
                System.Media.SystemSounds.Beep.Play();
            }
            else
            {
                if (TextDisplay.Text.Length == 1)
                {
                    TextDisplay.Text = "";
                }
                else
                {
                    TextDisplay.Text = TextDisplay.Text.Substring(0, TextDisplay.Text.Length - 1);
                }
            }
        }

        //Detetor da tecla enter
        if (e.KeyChar == 13)
        {
            Equal();
        }

        //Detetor da tecla Esc
        if (e.KeyChar == 27)
        {
            Clear();
        }
    
asked by anonymous 02.08.2016 / 15:55

2 answers

2

When you change the visibility and name of the method, you must maintain the parameters as well. Staying as follows

public void Detect(object sender, KeyPressEventArgs e)
    
02.08.2016 / 16:10
0

First you need to understand the problem: The body of your method with the signature private void TextDisplay_KeyPress(object sender, KeyPressEventArgs e) uses the variable " and " to detect the selected keys and make some comparisons within that method. >

When you changed the signature in the method to public void Detect() this variable ceased to exist, therefore the "The name" and "does not exist in the current context" error occurred p>

To solve this error you need to declare the same parameters:

public void Detect(object sender, KeyPressEventArgs e)
{
   ...
}
    
02.08.2016 / 16:25