Search Unity

[SOLVED] How to define a null/bool test?

Discussion in 'Scripting' started by ArachnidAnimal, Jul 31, 2015.

  1. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,808
    I don't know the correct terminology to explain what I am trying to do, but in C# you can check if an object is null by using the following:

    Code (csharp):
    1.  
    2. if (<object>)
    3. {
    4. Debug.Log("it is not null");
    5. }
    6. else
    7. {
    8. Debug.Log("the object is null");
    9. }
    10.  
    but when I try to do this using my own classes, i get the error
    "Cannot convert object to bool"

    I want to do this:
    Code (csharp):
    1.  
    2. public class Foo
    3. {
    4. //...
    5. }
    6.  
    7. Foo foo = new Foo();
    8. if (foo)
    9. {
    10. //...
    11. }
    12.  
    But this generates the above error message.
    So how can you define this operation in your class?
    Is there an overloaded operator to define?

    Thanks.
     
    Last edited: Jul 31, 2015
    jeffersonrcgouveia likes this.
  2. Deleted User

    Deleted User

    Guest

    if (myObject == null)
    // do stuff
     
    ArachnidAnimal likes this.
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,808
    I realize you can do it that way,
    but is there a way just do this:
    if ( ! myObject)
    {
    }
    I'm interested in how to do that in C#.
     
  4. Deleted User

    Deleted User

    Guest

    Nope you can't.
     
    ArachnidAnimal likes this.
  5. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    You can override the == operator for your class and do a reference equivalence check inside. See this StackOverflow answer for code example: http://stackoverflow.com/a/4219274

    Personally, I like doing (if myObj == null), because it makes the code explicitly clear about what's going on. :)

    Owen
     
    ArachnidAnimal likes this.
  6. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,808
    Ok thanks. i started to suspect you couldn't because i couldnt find any examples using it. I wonder how the C# compiler is able to do it using UnityEngine.GameObject

    you can do this:
    Code (csharp):
    1.  
    2. UnityEngine.GameObject myUnityEngineObject;
    3. if (myUnityEngineObject)
    4. {
    5. }
    6.  
    So i wonder how it can handle that, but not your own defined class.
     
  7. Deleted User

    Deleted User

    Guest

    That's different from what the OP is talking about.

    I can't test whether this works or not but if it works it would be due to implicit conversation from GameObject to bool. You could do the same if you wanted to although it would probably be more hassle to implement it than to check using the way I showed you.

    https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx
     
    ArachnidAnimal likes this.
  8. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,808
    OK, THANKS!
    That was exactly what I was hoping for. It works now when I defined the following in my class:

    Code (csharp):
    1.  
    2.   public static implicit operator bool (MyObject myObject)
    3.      {
    4.        return (myObject != null);
    5.      }
    6.  
    now i can do the null check by the following:
    Code (csharp):
    1.  
    2. if (myObject)
    3. {
    4. //...
    5. }
    6.