0

I have created an application that simply takes two inputs (numOne, numTwo) using a text box and then the method that is within the class multiplies it and returns the result. I can't get visual studio to use the user input numOne or numTwo to passed to the method to multiply them. The method multiplies numOne x numTwo and returns the result. How can I get the textbox user input to be passed to the method?

I have tried –

private void userInput_TextChanged(object sender, EventArgs e)
{
   Class1 multiply = new Class1();
   multiply.NumOne = userInput;  
}

private void Okay_Click(object sender, EventArgs e)
{
   Class1 Co = new Class1();
   MessageBox.Show(Co.Multiply().ToString());  
}
2
  • You have to learn about scope. The class you created in the TextChanged event can only be seen by code within that code block. Commented Jul 25, 2018 at 19:21
  • userInput looks like a control, so you probably want the userInput.Text property. Use Integer.TryParse to get the proper value out of it. Commented Jul 25, 2018 at 19:26

2 Answers 2

1

You create Class1 multiply and set NumOne. Then you create Class1 Co and call Multiply()

Co doesn't know anything about the value of multiply's NumOne property.

Like "Hey Paul my number is 12 don't tell Lisa". "Hey Lisa please Multiply the number you don't know".

One object encapsulates its data from other objects. Data that belongs together should be placed inside the same object.

E.g.:

public class Form1 : Form
{
    private Class1 multiplyer;

    public Form1()
    {
        this.multiplyer = new Class1(); // one single instance of Class1 shared between everything inside Form1
    }

    private void UserInput_TextChanged(object sender, EventArgs e)
    {
       this.multiplyer.NumberOne = int.Parse(UserInput.Text);  
    }

    // ...

    private void Okay_Click(object sender, EventArgs e)
    {
       MessageBox.Show(this.multiplyer.Multiply().ToString());  
    }
}

Take a look at Microsofts explanation of 'class vs object'

Sign up to request clarification or add additional context in comments.

Comments

0

remove textChanged, but modify here:

private void Okay_Click(object sender, EventArgs e)
{
    Class1 Co = new Class1();

    Co.NumOne= double.parse(input1.Text);
    Co.NumTwo= double.parse(input2.Text);
    MessageBox.Show(Co.Multiply().ToString());
}

if you are using 1 input only, you have to let user input like his : 1.5+99.1, then

private void Okay_Click(object sender, EventArgs e)
{
    Class1 Co = new Class1();
    var numbers=input1.Text.Split('+');
    Co.NumOne= double.parse(numbers[0]);
    Co.NumTwo= double.parse(numbers[1]);
    MessageBox.Show(Co.Multiply().ToString());
}

2 Comments

Hi thanks. Why the use of parse. Is the text box the string type and the user input the integer? Therefore converting the string in to and integer?
yes, if you need integer only, then use Int32.Parse()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.