Click here to Skip to main content
15,899,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the codes below, one thing i dont get it is what should i return for object data type?

C#
get
{
    object loValue;
    loValue = this.CurrentRow["LeaveCode"];
    if (loValue == DBNull.Value)
    {
        return ??? //here
    }
    else
    {
        return (System.String)loValue;
    }
}
set
{
    this.CurrentRow["LeaveCode"] = value;
}
Posted
Comments
Vedat Ozan Oner 5-Mar-14 3:05am    
null

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.
 
Share this answer
 
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...
 
Share this answer
 
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.
 
Share this answer
 
return empty string

C#
get
            {
                object loValue;
                loValue = this.CurrentRow["LeaveCode"];
                if (loValue == DBNull.Value)
                {
                    return string.empty;
                }
                else
                {
                    return (System.String)loValue;
                }
            }
            set
            {
                this.CurrentRow["LeaveCode"] = value;
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900