[英]Assign string value for decimal value
_item = new OutTYonetimOzet();
_item.Banka = Convert.ToDecimal(" ");
liste.Add(_item);
There is a list called liste
. 有一个名为
liste
的列表。 In List item Banka
named element is decimal value. 在列表项中,
Banka
命名元素是十进制值。 I want to show the empty string when I show it on the screen. 我想在屏幕上显示空字符串时显示空字符串。 But this code is getting an error that can not be cast.
但是这段代码得到了一个无法投射的错误。 What is the problem.
问题是什么。
Error message is: 错误信息是:
Input string was not in a correct format.
输入字符串的格式不正确。
There's no such thing as a "blank decimal". 没有“空白小数”这样的东西。
decimal
cannot have a value that is "blank" - it always has a numeric value. decimal
不能具有“空白”值 - 它始终具有数值。 Convert.ToDecimal(" ")
is nonsensical - there is nothing it can return that makes sense. Convert.ToDecimal(" ")
是荒谬的 - 没有什么可以返回它是有道理的。
You could try using a Nullable<decimal>
(aka decimal?
) perhaps; 您可以尝试使用
Nullable<decimal>
(也就是decimal?
); ie 即
public decimal? Banka {get;set;}
and 和
_item.Banka = null;
You can also use the decimal.TryParse
instead of Convert
. 您也可以使用
decimal.TryParse
而不是Convert
。 With this technique you can check if the string is valid. 使用此技术,您可以检查字符串是否有效。
_item = new OutTYonetimOzet();
decimal val = decimal.MinValue;
if (decimal.TryParse(" ", out val))
{
_item.Banka = val;
}
else
{
//your default goes here
_item.Banka = 0;
}
liste.Add(_item);
and as Mark suggested I would use Nullable<decimal>
and use null as default value. 并且Mark建议我使用
Nullable<decimal>
并使用null作为默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.