Question

Code :

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

some arguments are not valid?

Était-ce utile?

La solution

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;

Autres conseils

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.
}

Just as an additional answer, you can now declare out parameters inline.

if (decimal.TryParse(myRow[0].ToString(), out decimal outParamName))
{
    // do stuff with decimal outParamName
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top