Link to home
Start Free TrialLog in
Avatar of Hojoformo
Hojoformo

asked on

C# - how to add decimal types together

Simple question.   How can I add 2 decimal data types together?

string x;
decimal y;
decimal w;
     while (dtrResults1.Read())
            {
             x = dtrResults1.GetString(0);
             w = Decimal.Parse(x);
             y = y + w;
            }

I get an error message with the code above.  
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

What language? Where? server? client?

What is the error you get?

Cd&
Avatar of KeirGordon
KeirGordon

I think decimal is not a basic datatype and must be instantiated.  I assume you are getting a null reference exception.  Instatiate y.

string x;
decimal y = new decimal(0);
decimal w;
     while (dtrResults1.Read())
            {
             x = dtrResults1.GetString(0);
             w = Decimal.Parse(x);
             y = y + w;
            }
Also, probably some code like this is much more efficient..... Should do the same thing as yours.

double y = 0;
while (dtrResults1.Read())
     y += dtrResults1.GetDouble(0);
         
ASKER CERTIFIED SOLUTION
Avatar of KeirGordon
KeirGordon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial