코드 :이 Decimal.TryParse의 문제점은 무엇입니까?
Decimal kilometro = Decimal.TryParse(myRow[0].ToString(), out decimal 0);
일부 인수가 유효하지 않습니다?
코드 :이 Decimal.TryParse의 문제점은 무엇입니까?
Decimal kilometro = Decimal.TryParse(myRow[0].ToString(), out decimal 0);
일부 인수가 유효하지 않습니다?
out decimal 0
은 유효한 매개 변수가 아닙니다. 0
은 유효한 변수 이름이 아닙니다.
decimal output;
kilometro = decimal.TryParse(myRow[0].ToString(), out output);
, 반환 값은 bool
될 것입니다 - 변수의 이름에서, 당신의 코드는 아마되어야합니다 : 당신이 kilometro
을 반환하려면 때문에
if(decimal.TryParse(myRow[0].ToString(), out kilometro))
{
// success - can use kilometro
}
, 당신은 할 수 있습니다 :
decimal kilometro = 0.0; // Not strictly required, as the default value is 0.0
decimal.TryParse(myRow[0].ToString(), out kilometro);
return kilometro;
음은 decimal.TryParse
는 bool
형식을 반환하는 - 그래서 당신은 같은 것을 수행해야합니다
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;
}
TryParse 문을 올바르게 사용하는 방법은 다음과 같습니다. 먼저 소수점을 선언 한 다음 TryParse 메서드에 전달해야합니다. TryParse가 성공하면 kilometro
이 새 값이되고, 그렇지 않으면 0이됩니다. 나는 그것이 당신이 원하는 결과라고 믿는다.
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.
}
필자는 성공적인 'bool'반환 유형 대신 'kilometro'도 'double'이라고 생각합니다. –
하지만 구문 분석에 실패하면 0을 반환하고 싶습니다 ... – markzzz
@AdamHouldsworth - 동의 ... – Oded