none
Cannot convert null to 'int' because it is a value type--need help

    Question

  • hi, i need some advice regarding the code that i have created,

    Code Snippet


       public static int IntToHex(int no)
            {
                try
                {
                    string hex = no.ToString("x2");
                    return Convert.ToInt32(hex);
                }
                catch (OverflowException ofe)
                {
                    Console.WriteLine("{0}",ofe.StackTrace);
                    return null;

                }
                catch (StackOverflowException soe)
                {
                    Console.WriteLine("{0}", soe.StackTrace);
                    return null;
               
                }
           
            }


    the error that is shown on the compiler is:

    Cannot convert null to 'int' because it is a value type

    any advice is appreciated .thanks in advance
    Tuesday, June 26, 2007 1:24 AM

Answers

  • Since you 'know' that the operation can fail i would recommend to use a TryGet pattern instead of returning null on failure...

    Code Snippet

    public static bool TryGetHex(int no, out int result)
    {
     result = 0;

     try
     {
      string hex = no.ToString("x2");
      result = Convert.ToInt32(hex);
     }
     catch (OverflowException oe)
     {
      Console.WriteLine(oe.StackTrace);
     }
    }


    And then you can use it as following:

    Code Snippet

    int no = 12345678;
    int result;
    if (TryGetHex(no, out result))
    {
     // success, do something with result
    }
    else
    {
      // failure...
    }


    Tuesday, June 26, 2007 5:53 AM
  • Hi,

     

    You can better have a default value, say -1 (which you know that it wont be the output in your application usually), to return on getting exception.

     

    Regards,

    Tuesday, June 26, 2007 9:36 AM

All replies

  • Your IntToHex function is declared as type 'int' but in your two catch blocks you return 'null'.  The value 'null' is not an integer and cannot be converted into something that is an int.
    Tuesday, June 26, 2007 1:35 AM
  • You can still use this, you just need to return a Nullable<int> or you can use int?

     

    Code Snippet

    public static Nullable<int> IntToHex(int no)

    {

    try

    {

    string hex = no.ToString("x2");

    return Convert.ToInt32(hex);

    }

    catch (OverflowException ofe)

    {

    Console.WriteLine("{0}", ofe.StackTrace);

    return null;

    }

    catch (StackOverflowException soe)

    {

    Console.WriteLine("{0}", soe.StackTrace);

    return null;

    }

    }

     

    Tuesday, June 26, 2007 2:11 AM
  • thanks for ur advice.however if i try that way compiler would show me :   Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
    Tuesday, June 26, 2007 2:48 AM
  • Wherever you declare the IntToHex you would also have to use the same type:

     

    Code Snippet

    [STAThread]

    static void Main()

    {

    //Application.EnableVisualStyles();

    //Application.SetCompatibleTextRenderingDefault(false);

    //Application.Run(new ExcelRWForm());

    int? a = IntToHex(2);

    }

    public static int? IntToHex(int no)

    {

    try

    {

    string hex = no.ToString("x2");

    return Convert.ToInt32(hex);

    }

    catch (OverflowException ofe)

    {

    Console.WriteLine("{0}", ofe.StackTrace);

    return null;

    }

    catch (StackOverflowException soe)

    {

    Console.WriteLine("{0}", soe.StackTrace);

    return null;

    }

    }

     

    Tuesday, June 26, 2007 3:46 AM
  • Since you 'know' that the operation can fail i would recommend to use a TryGet pattern instead of returning null on failure...

    Code Snippet

    public static bool TryGetHex(int no, out int result)
    {
     result = 0;

     try
     {
      string hex = no.ToString("x2");
      result = Convert.ToInt32(hex);
     }
     catch (OverflowException oe)
     {
      Console.WriteLine(oe.StackTrace);
     }
    }


    And then you can use it as following:

    Code Snippet

    int no = 12345678;
    int result;
    if (TryGetHex(no, out result))
    {
     // success, do something with result
    }
    else
    {
      // failure...
    }


    Tuesday, June 26, 2007 5:53 AM
  • Hi,

     

    You can better have a default value, say -1 (which you know that it wont be the output in your application usually), to return on getting exception.

     

    Regards,

    Tuesday, June 26, 2007 9:36 AM
  • Hi,

    we can use this 

    Convert.ToInt32(hex, 16);

      public static int? IntToHex(int no)
            {
                try
                {
                    string hex = no.ToString("x2");
                    return
    Convert.ToInt32(hex, 16);                    
                }
                catch (OverflowException ofe)
                {
                    Console.WriteLine("{0}",ofe.StackTrace);
                    return null;

                }
                catch (StackOverflowException soe) 
                {
                    Console.WriteLine("{0}", soe.StackTrace);
                    return null;

                }

            }

    Tuesday, April 03, 2012 12:42 PM
  • The thread is almost five years old and has already been answered.

    If you would like to contribute to the community you would be better served answering questions asked within the past few days that have not already been answered.

    Tuesday, April 03, 2012 2:12 PM