2

I want to add two numbers. I am getting values from button in my textbox. I also succeeded in splitting string into substrings and store values of these substrings in variables. But i am not able to convert string type to integer type. That results in concatenation not in addtion. Note : I am using MVC to perform this task. And in model value1 and value2 is string type in model Here is my code snippet:

if (button == "1"){
    if (model.textBox == "" || model.textBox == null || model.textBox.ToLower().Contains("please enter value")){
        model.textBox = "1";
    } else {
        model.textBox += "1";
    }
}
if (button == "2") {
    if (model.textBox == "" && model.textBox == null) {
        model.textBox = "2";
    } else {
        model.textBox += "2";
    }
if (button == "+") {
    if (model.textBox == "" && model.textBox == null){
        model.errormsg = "Please enter a number ";
    } else {
        model.textBox += "+";
    }
if (button == "=") {
    if (model.textBox.Contains("+")) {
        model.value1 = (model.textBox.Split('+'))[0];
        int value1 = int.Parse(model.value1);
        model.value2 = (model.textBox.Split('+'))[1];
        int value2 = int.Parse(model.value2);
         model.textBox = model.value1 + model.value2;
    }
return View(model); 
1
  • 1
    Your adding the model values (which are typeof string), it the int values you declare - model.textBox = (value1 + value2).ToString(); Commented Jul 27, 2015 at 13:14

4 Answers 4

5

If i got you correctly all you need to do is:

model.textBox = (value1 + value2).ToString();
Sign up to request clarification or add additional context in comments.

6 Comments

Already my value are of string type. I want to convert them to integer. So that values must be added not concatenated.
Yes, but value1 is in and also value2 - after you added them it makes on the sum the ToSring() action.
Please provide me some more help and detail. I am not getting the desired result. It still concatenate the value 1 and value2, not adding value1 and value2.
please tell me where do you see the concatination? in which part or code snippet?
if (button == "=") { if (model.textBox.Contains("+")) { model.value1 = (model.textBox.Split('+'))[0]; model.value2 = (model.textBox.Split('+'))[1]; model.textBox = (model.value1 + model.value2).ToString();} this code snippet gives me concatenation result: if entered values are 1 and 2 then result would be 3 but this gives me result 12
|
1

model.textBox is of type string, so, when you do model.textBox += "1"; the only possible operation is concatenation.

To add them as integers you first need to convert your textBox to int.

Something like the following will work:

model.textBox = (int.Parse(model.textBox) + 1).ToString();

Comments

0

Try using int.Parse(textBox.Text).

It will crash if there is a non-numeric character in the string, but that's nothing a try-catch block won't fix.

Hope this helps!

Comments

0
 if (button == "=") {
    if (model.textBox.Contains("+")) {
        model.value1 = (model.textBox.Split('+'))[0];
        int value1 = int.Parse(model.value1);
        model.value2 = (model.textBox.Split('+'))[1];
        int value2 = int.Parse(model.value2);
         model.textBox = model.value1 + model.value2;
    }

Replace above code snippet with below code snippet:

if (button == "=")
            {
                if (model.textBox.Contains("+"))
                {
                    model.value1 = (model.textBox.Split('+'))[0];

                    model.value2 = (model.textBox.Split('+'))[1];

                    model.textBox1 = (int.Parse(model.value1) + int.Parse(model.value2)).ToString();


                }

Comments

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.