简体   繁体   中英

Visual C# Error: System.Windows.Forms.Button does not contain a definition for ToInt32

Im working on a decimal to binary converter that works perfectly on a console and then i get these errors about my core math operations:

System.Windows.Forms.Button does not contain a definition for ToInt32 and no extension method ToInt32 accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Line:93

No overload for method 'ToString' takes 2 arguments Line:94

System.Windows.Forms.Button does not contain a definition for 'ToInt32' and no extension method 'ToInt32' accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) Line:103

Here is the code:

    public void Convert_Click(object sender, EventArgs e)
    {
        string Input;
        bool IsNotBinary;
        string Answer;
        Start:
        Input = UserInput.Text;
        int InputLength = Input.Length;
        if (InputLength > 10)
        {
            UserInput.Text = "Overflow";
            goto Start;
        }
        int Int;
        bool IsANumber = int.TryParse(Input, out Int);
        if (IsANumber == false)
        {
            UserInput.Text = "Invalid Character";
            goto Start;
        }
        IsNotBinary = Input.Contains("3");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("4");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("5");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("6");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("7");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("8");
        if (IsNotBinary == true)
        {
            goto End;

        }
        IsNotBinary = Input.Contains("9");
    End:

        if (IsNotBinary == true)
        {

            // decimal to binary
            int InputInt = Convert.ToInt32(Input); // converts the string "Input" to the int "InputInt"
            Answer = Convert.ToString(InputInt, 2);
            UserInput.Text = Answer;

        }

        else
        {

            // binary to decimal
            Answer = Convert.ToInt32(Input, 2).ToString();
            UserInput.Text = Answer;

        }
        Console.ReadLine();
        goto Start;
    }

    public void QuitButton_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }        
}

}

The error message is quite clear:

System.Windows.Forms.Button' does not contain a definition for 'ToInt32'

My psychic debugger tells me that you have a class level button variable named Convert , so you're not calling the ToInt32 method on the static Convert class as your button is hiding it. Either rename the button or fully qualify the name, ie, System.Convert.ToInt32() .

Edit:

Ok, I guess I didn't need my psychic debugger after all. Your event handler tells me all I need to know:

public void Convert_Click(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM