C# conversion of a private void to public void

Asked

Viewed 82 times

0

Hello everyone I need to know how I can convert this code to public void. Visual Studio shows error in variable and.

Error message: "The name 'e' does not exist in the Current context"

I just did it ctrl+c and ctrl+v of:

private void TextDisplay_KeyPress(object sender, KeyPressEventArgs e) 

To:

public void Detect() 

Down with 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();
        }
  • The change you are reporting in the question should not cause any problems. Anyway only with this stretch it is not possible to identify what happens. Put more details about the error (message, line) and other relevant snippets.

  • @bigown I limit to making Ctrl c Ctrl v code above private void TextDisplay_KeyPress(object sender, KeyPressEventArgs e) for public void Detect() made Ctrl s and appeared the error above

  • place public void Detect(Object Sender, Keypresseventargs and)

  • @Pablovargas Thank you for that as an answer

2 answers

2


When changing the visibility and method name, you should keep the parameters as well. Being as follows

public void Detect(object sender, KeyPressEventArgs e)

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 this method.

When you changed the signature in the method to public void Detect() this variable no longer exists, therefore the error occurred "The name 'e' does not exist in the Current context".

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

public void Detect(object sender, KeyPressEventArgs e)
{
   ...
}

Browser other questions tagged

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