对象数据类型返回什么? [英] What to return for object data type?

查看:81
本文介绍了对象数据类型返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,有一点我不明白我应该为对象数据类型返回什么?



I have the codes below, one thing i dont get it is what should i return for object data type?

get
{
    object loValue;
    loValue = this.CurrentRow["LeaveCode"];
    if (loValue == DBNull.Value)
    {
        return ??? //here
    }
    else
    {
        return (System.String)loValue;
    }
}
set
{
    this.CurrentRow["LeaveCode"] = value;
}

推荐答案

如果是DBNull,你可以决定返回什么。它可以是一些验证消息或偶数

String.Empty 但取决于您的要求。
You can decide on what to be returned if its DBNull .It can be some Validation Message or Even
String.Empty too But depends on your Requirement.


这取决于属性的类型 - 这将取决于您的要求似乎是字符串。

在这种情况下,你只有两个选项:return null 或一个空字符串。

系统不允许您返回任何其他数据类型,字符串是一个密封类,因此您不能返回派生类,并且行中没有信息要返回,没有好字符串可以返回。 />


取决于你:如果你习惯性地检查空值,那就用它。如果不这样,那么空字符串意味着外部代码在空引用异常上崩溃的可能性较小...
That depends on the type of teh property - which would appear to be string.
In that case, you have only two options: return null or an empty string.
The system will not allow you to return any other datatype, string is a sealed class so you can't return a derived class, and with no information in the row to return there is no "good" string you can return.

Depends on you: if you habitually check for nulls, then use that. If you don't then an empty string means there is less chance of your external code crashing on a null reference exception...


它取决于DBNull(数据中的空值)来源)代表。在大多数情况下,它只是意味着数据不存在,因此您应该在代码中返回一个缺失值等价物 - 对于引用类型 null



DBNull的整个概念让我感到困惑 - 为什么在从数据提供程序读取空单元格时,框架不仅仅为您提供框架 null ?这是你想要99%的时间,这意味着演员在这样的访问器中正常工作。
It depends what a DBNull (a null value in your data source) represents. In most cases it just means that the data is not present and therefore you should return a 'missing value' equivalent in your code – which for reference types is null.

The whole concept of DBNull confuses me somewhat – why does the framework not just give you the framework null when reading null cells from a data provider? It's what you want 99% of the time and it would mean the cast worked correctly in accessors like this.


这篇关于对象数据类型返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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