Comparing .NET values

When comparing .NET variables, including Enums, you cannot use C/AL comparison operators. To compare .NET variables, you must use the Equals method (of the System.Object type) that all .NET types implement or inherit. So, instead of IF var1 = var2, or IF var1 = var1.EnumValue (in case of an Enum), just write IF var1.Equals(var2), or IF var1.Equals(var1.EnumValue).

I see this mistake often being made or attempted by developers, even though it has been documented inside .NET Interoperability documentation since it was introduced with 2009 R2.

This Post Has 2 Comments

  1. Debp's avatar
    Debp

    So how would you use these types within a case statement?

    1. Vjeko's avatar
      Vjeko

      Easy:
      CASE TRUE OF
      Something.Equals(whatever): BEGIN..END;
      // ETC, you get my gist
      END;

Leave a Reply