Search Unity

Cannot implicitly convert type `long?' to `int'.

Discussion in 'Scripting' started by will_brett, Sep 5, 2015.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hi there,

    I've come across an error which says: Cannot implicitly convert type `long?' to `int'.

    I've had a look online and lots of places are saying use Convert.ToInt32(). but that doesn't to seem to work with unity.

    I guess its a long because that is what the server used to store scores is setup to use. However I would like to just use an int.

    Any ideas?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I can come up with many ways to make that error appear. As I'm not a magician, I can't read your code over the internet. So unless you post whatever's causing the issue, no ideas.

    One note, though; the type long? means that the value is "a long or null". If you have a struct Foo, the type Foo? is either Foo, or null. So if you're getting a long?, and want to convert it to an int, you could do something like:

    Code (CSharp):
    1. private int ToInt(long? value) {
    2.     if (value.HasValue)
    3.         return Convert.ToInt32(value);
    4.  
    5.     return 0; //the input value was null
    6. }
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or just

    Code (CSharp):
    1. (int)myLong
     
    irshadgirachirshu likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    If an external library is using long? instead of long, assume that they're doing it for a reason, and do the HasValue check, or you'll end up shiping a build that crashes due to InvalidOperationExceptions.

    Since long? is a reference type, it's default value is null, so the probability of a long? being null is really high - it just needs to not have been done anything with.
     
    Ru2L021, Ryiah and Kiwasi like this.
  5. moscas99

    moscas99

    Joined:
    Jun 13, 2017
    Posts:
    7
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
  7. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    As long as we're in a 3 year old thread anyway ...
    Nullable types are not reference types. long? is a value type.
    Nullable types are just a struct containing a value of the generic type (which must be a value type) and a bool to indicate whether it has been assigned (or is "null"). Its default value is "null" because the default value of bool is false.

    Code (CSharp):
    1. public struct Nullable<T> where T : struct
    2.     {
    3.         public Nullable(T value);
    4.  
    5.         public bool HasValue { get; }
    6.         public T Value { get; }
    7.  
    8.         public override bool Equals(object other);
    9.         public override int GetHashCode();
    10.         public T GetValueOrDefault();
    11.         public T GetValueOrDefault(T defaultValue);
    12.         public override string ToString();
    13.  
    14.         public static implicit operator T? (T value);
    15.         public static explicit operator T(T? value);
    16.     }
     
    Last edited: Mar 16, 2019
    Baste and TaleOf4Gamers like this.