0
votes

I have just tried everything and nothing will work. Have created a new class and set all my coding in there and trying to call the public method from my form1.cs but in my class1 my textbox1 and sender are in red writing.

Code for Form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    string text;
    double operand_1, operand_2, solution;
    char Operator;

    #region Form Code
    private void Form1_Load(object sender, EventArgs e)
    {
        operand_1 = operand_2 = solution = 0;
        text = "0";
        Operator = '=';
    }
    #endregion

    #region Number Buttons
    private void numbers_Click(object sender, EventArgs e)
    {
        Class1 cls = new Class1();
        cls.Numbers(textBox1.Text);
    }
    #endregion

    #region Operator Buttons
    private void operators_Click(object sender, EventArgs e)
    {
        Class1 cls1 = new Class1();
        cls1.Operations();
    }
    #endregion
}

Then here is my coding for my coding for my Class1:

public class Class1
{
    string text;
    double operand_1, operand_2, solution;
    char Operator;

    public void Numbers(string text)
    {
        Button button = (Button)sender;
        text += button.Text;
        while (text != "0" && text[0] == '0' && text[1] != '.')
            text = text.Substring(1);
        textBox1.Text = text;
        return text;
    }

    public void Operations()
    {
        if (Operator != '=')
        {
            operand_2 = double.Parse(textBox1.Text);

            switch (Operator)
            {
                case '+': solution = operand_1 + operand_2;
                    break;
                case '-': solution = operand_1 - operand_2;
                    break;
                case '*': solution = operand_1 * operand_2;
                    break;
                case '/': solution = operand_1 / operand_2;
                    break;
            }
            Operator = '=';
            textBox1.Text = solution.ToString();
        }
        operand_1 = double.Parse(textBox1.Text);
        Operator = char.Parse(((Button)sender).Text);
        text = "0";
    }
}

Sender error: cannot resolve symbol 'sender' Textbox error: cannot resolve symbol 'textBox1' not sure if it should be public string or void

:'(

Any help would do pls!!!

2
Ehhh.. you moved code from the event handler to a seperate class, but are not giving the seperated code the "sender" parameter from the event handler. Hence, the "sender" variable is undeclared in the "Numbers" methodMarvin Smit

2 Answers

0
votes

You need to make your function Numbers return a string. Then in your call to it, from the form, set the textbox text to the return value.

private void numbers_Click(object sender, EventArgs e)
{
    Class1 cls = new Class1();
    textBox1.Text = cls.Numbers(textBox1.Text);
}
public string Numbers(string text)
{
    Button button = (Button)sender;
    text += button.Text;
    while (text != "0" && text[0] == '0' && text[1] != '.')
        text = text.Substring(1);

    return text;
}

public void Operations(string textBoxText, string buttonText)
{
    string returnText = "";

    if (Operator != '=')
    {
        operand_2 = double.Parse(textBoxText);

        switch (Operator)
        {
            case '+': solution = operand_1 + operand_2;
                break;
            case '-': solution = operand_1 - operand_2;
                break;
            case '*': solution = operand_1 * operand_2;
                break;
            case '/': solution = operand_1 / operand_2;
                break;
        }
        Operator = '=';
        returnText = solution.ToString();
    }
    operand_1 = double.Parse(textBoxText);
    Operator = char.Parse(buttonText);
    text = "0";
    return returnText;
}

private void operators_Click(object sender, EventArgs e)
{
    Class1 cls1 = new Class1();
    cls1.Operations(textBox1.Text, ((Button)sender).Text);
}
0
votes

I could be wrong but be sure that the textbox modifiers is set to public if its in red I had that and that's how I fixed mine for that part at least.