如何避免在分配给对象时十进制值不为空可变为空? [英] How to avoid decimal value not null becoming nullable on assigning to an object?

查看:71
本文介绍了如何避免在分配给对象时十进制值不为空可变为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免在分配给对象时十进制值不为空可变为空?我在数据集中具有值,但在使用fieldname分配给对象时它可以变为可为空。如何避免这个问题。

How to avoid decimal value not null becoming nullable on assigning to an object? I am having the value in the dataset but its becoming nullable on assigning to object with fieldname. How to avoid the issue.

推荐答案

小数总是有一些默认值。如果您需要可空类型 [ ^ ]十进制,你可以用十进制吗?然后你可以做myDecimal.HasValue
A decimal will always have some default value. If you need to have a nullable type[^] decimal, you can use decimal?. Then you can do myDecimal.HasValue


使用哪一个适合你的问题..



Use which one suits for your issue..

decimal value = default(decimal); //0
        object objvalue = value; // object will be having 0

        decimal? nullableValue = null;  // null
        object objval = nullableValue.HasValue ? nullableValue.Value : default(decimal);

        decimal val = default(decimal);  //0
        decimal.TryParse(DBNull.Value.ToString(), out val); // if null it will be assigned as 0 , else the value
        object valueFromDataset = val;


这篇关于如何避免在分配给对象时十进制值不为空可变为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆