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.
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.
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;
}
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);
double y = 0;
while (dtrResults1.Read())
y += dtrResults1.GetDouble(0);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is the error you get?
Cd&