What is wrong on this Decimal.TryParse?

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;

Well, the decimal.TryParse returns a bool type - so you need to do something like:

Decimal kilometro;

// if .TryParse is successful - you'll have the value in "kilometro"
if (!Decimal.TryParse(myRow[0].ToString(), out kilometro)
{ 
   // if .TryParse fails - set the value for "kilometro" to 0.0
   kilometro = 0.0m;
} 

The correct usage of the TryParse statement is given below. You must declare the decimal first and then pass it into the TryParse method. If the TryParse succeeds, kilometro will be the new value, otherwise it will be zero. I believe that was your desired outcome.

decimal kilometro = 0;
if (Decimal.TryParse(myRow[0].ToString(), out kilometro))
{
   //The row contained a decimal.
}
else {
   //The row could not be parsed as a decimal.
}

Tags:

C#

Tryparse