How to cast Null object.

Hi all,

I am wondering if I can do casting for null object. Lets see the following code:

int i;

object o;

o = null;

i = (int)o;

I am getting error that "Object reference not set to an instance of an object."

Is this possible by any other way

Thank you



Answer this question

How to cast Null object.

  • Heidi

    I made one more change to my DbUtility class - maked the method as public. I also marked it as an answer, as I assume it is.

    cheers back at ya!



  • Qwonfu

    Hi Blair,

    Thanks again.

    if I am using with second option of your above example, having following error:

    "The as operator must be used with a reference type ('int' is a value type) ".

    Regards


  • Dmitry Titov MSFT

    I am thinking you can make a globally available static method:

    public static class DbUtility
    {
        static public object ValueToDefault(object value, object defaultValue) 
       
            return value == DBNull.Value defaultValue : value; 
        }
    }
     

    int
    iNotNullable = (int) DbUtility.ValueToDefault(dsTrims.Tables[0].Rows[ i ]["model_id"], int.MinValue);
    int
    iNullable = (intDbUtility.ValueToDefault(dsTrims.Tables[0].Rows[ i ]["model_id"], null);



  • John7

    int iNotNullable = (int) dsTrims.Tables[0].Rows[ i ]["model_id"] == DBNull.Value
    int.MinValue : dsTrims.Tables[0].Rows[ i ]["model_id"];

    But see the post I just made above. .. I have made a couple of edits in it. . . make sure you get it now with my latest edits.



  • Silvia Elena

    take a look at nullable types -

    int iNullable;
    int iNotNullable = 0;
    object obj = null;
    iNullable = obj as int ;
    if (obj is int)
    iNotNullable = (
    int)o;


  • Hoot

    Thank you very much Blair,

    You are too good. I cant express my feeling.

    Have a great time.

    Cheers.....


  • SameerM

    Ow Nullable types true. I forgot them sorry... :)

    Happy coding...


  • Phillb

    Hi Blair,

    I am not getting about int .

    Actuallu I am getting error while using dataset as following:

    for(i=0...............{

    int intModelId = (int)dsTrims.Tables[0].Rows[ i ]["model_id"]; }

    where "dsTrims" is dataset.

    In this code if "model_id" is null then getting error that "casting is not valid".

    How can I solve this error without writing lots of code. I can do it by checking like:

    if(dsTrims.Tables[0].Rows[ i ]["model_id"] is DBNULL){ ....... }

    But I have to write/check this statement for each & every column.

    Any other possible way

    Thank you


  • Coolnet1savvy

    int iNullable = (dsTrims.Tables[0].Rows[ i ]["model_id"] == DBNull.Value
    null : dsTrims.Tables[0].Rows[ i ]["model_id"]) as int ;

    int is a Nullable Integer

    or, if non-nullable int and want DBNull to be int.MinValue:

    int iNotNullable = (dsTrims.Tables[0].Rows[ i ]["model_id"] == DBNull.Value
    int.MinValue : dsTrims.Tables[0].Rows[ i ]["model_id"]) as int;


  • Grahamrounce

    Hello, Null casting is invalid in .NET Because null objects doesn't has a memory area. No start point. No length. It is impossible. Happy coding...
  • How to cast Null object.