Carlos
Carlos

Reputation: 1

Calculating user input

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());  
}

Upvotes: 0

Views: 69

Answers (2)

Dongdong
Dongdong

Reputation: 2498

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());
}

Upvotes: 0

The-First-Tiger
The-First-Tiger

Reputation: 1574

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'

Upvotes: 1

Related Questions