What is wrong on this Decimal.TryParse?

markzzz picture markzzz · Dec 14, 2012 · Viewed 47.5k times · Source

Code :

Decimal kilometro = Decimal.TryParse(myRow[0].ToString(), out decimal 0);

some arguments are not valid?

Answer

Oded picture Oded · Dec 14, 2012

out decimal 0 is not a valid parameter - 0 is not a valid variable name.

decimal output;
kilometro = decimal.TryParse(myRow[0].ToString(), out output);

By the way, the return value will be a bool - from the name of the variable, your code should probably be:

if(decimal.TryParse(myRow[0].ToString(), out kilometro))
{ 
  // success - can use kilometro
}

Since you want to return kilometro, you can do:

decimal kilometro = 0.0; // Not strictly required, as the default value is 0.0
decimal.TryParse(myRow[0].ToString(), out kilometro);

return kilometro;