-3

i am looping through some nodes and getting the charge and adding them together. the charge is of type string though.

first time it loops string charge = "309",

second time it loop string charge = "38";

 `Looping through list of nodes
 {
   saBillDetail.TotalServiceUsage += totalSvcNode.InnerText;
 }
`

I would have expected that I could add them together, but they are being concatenated instead like this:

`charge + charge = '30938'`

How can I force these strings to be treated as numbers? and get output like this at the end of the loop

`charge + charge = '347'`
4
  • 3
    int.Parse()... Commented Jul 29, 2015 at 21:19
  • 2
    I tend to prefer the TryParse() methods, as they don't raise exceptions. There are a number of ways to convert strings to numbers, though. Commented Jul 29, 2015 at 21:23
  • use google to do a MSDN search on int.Parse() Method show a little bit more effort on your end. Google is your friend Commented Jul 29, 2015 at 21:25
  • 1
    If you can delete this message ;) maybe you get the peer pressure badge Commented Jul 29, 2015 at 21:29

3 Answers 3

4

As people allready answered this maybe another solution So you don't get errors

    private static int AddTwoStrings(string one, string two) 
    {
        int iOne = 0;
        int iTwo = 0;
        Int32.TryParse(one, out iOne);
        Int32.TryParse(two, out iTwo);
        return iOne + iTwo;
    }

Or if you want a string result.

private static String AddTwoStrings(string one, string two) 
{
    int iOne = 0;
    int iTwo = 0;
    Int32.TryParse(one, out iOne);
    Int32.TryParse(two, out iTwo);
    return (iOne + iTwo).ToString();
}

EDIT:

As Alexei Levenkov stated you could/should handle exceptions. Maybe something like this will help you during development

    private static int AddTwoStrings(string one, string two) 
    {
        int iOne = 0;
        int iTwo = 0;
        bool successParseOne = Int32.TryParse(one, out iOne);
        bool successParseTwo = Int32.TryParse(two, out iTwo);
        if (!successParseOne) 
        {
            throw new ArgumentException("one");
        }
        else if(!successParseTwo)
        {
            throw new ArgumentException("two");
        }

        return (iOne + iTwo);
    }

So when you have a wrong number you will be notified if you use try/catch

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

6 Comments

Although it is not the best question, I think this is a good answer.
I guess this one will be closed ;)
Generally it is bad idea to ignore results of TryParse... On other hand for Javascript / stringly typed code errors don't matter.
@AlexeiLevenkov okay good point. I added a sort of error handling. I firstly assumed that he then wanted to receive a valid number but giving him the choice is maybe a better soluton
@Bongo, your second chunk of code should have string as the return type.
|
2

You need to parse the numbers from the strings:

string number1 = "309";
string number2 = "38";

int result = int.Parse(number1) + int.Parse(number2);

Then you can set the text equal to that string representation:

string addResult = result.ToString();

Note: Int32.Parse() will throw an exception if the format isn't correct. Consider using Int32.TryParse() for a bool way to capture an impossible parsing.

Comments

2

You will need to convert your strings to integer, add them and convert the result back to string:

int sum = 0;
foreach (string strNumber in strNumberCollection)
{
    int number;
    if (int.TryParse(strNumber, out number))
        sum += number;
}

string total = sum.ToString();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.