Operator== overload problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony

    Operator== overload problem

    I have an operator== overload that compares two items and returns a new
    class as the result of the comparison (instead of the normal bool)

    I then get an ambiguous operater compile error when I attempt to check to
    see if the object is null:
    "The call is ambiguous between the following methods or properties:
    'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
    ==(TestObject, TestObject)"

    Does anyone have any idea how to correct this problem?

    Example source follows:

    Thanks,
    Tony

    internal class Condition
    {
    }

    internal class TestObject
    {
    public static Condition operator==(Test Object oTestObject, string
    sValue)
    {
    return new Condition(); // a stub for illistration purposes
    }

    public static Condition operator!=(Test Object oTestObject, string
    sValue)
    {
    return new Condition(); // a stub for illistration purposes
    }

    public static Condition operator==(Test Object oTestObject1, TestObject
    oTestObject2)
    {
    return new Condition(); // a stub for illistration purposes
    }

    public static Condition operator!=(Test Object oTestObject1, TestObject
    oTestObject2)
    {
    return new Condition(); // a stub for illistration purposes
    }

    public override int GetHashCode()
    {
    return base.GetHashCod e();
    }

    public override bool Equals(object aObject)
    {
    return base.Equals(aOb ject);
    }

    }

    internal class Test
    {
    void Testmethod()
    {
    TestObject oTestObject = null;

    if (oTestObject == null) // compile error occurs on this line
    oTestObject = new TestObject();
    }
    }


  • Dale

    #2
    RE: Operator== overload problem

    Yes. Don't overload == for anything except value types.

    I don't understand why you would return other than a bool? If you want a
    method to return an instance of a class based upon the comparison, then write
    a method that calls the comparison and returns the class instance based on
    the result but don't overload the comparison.

    When you do have to overload equivelence in objects (and I recommend doing
    it more often than not) overload Object.Equals() , but you still have to
    return a bool. You can't change the return type in an overload. You could,
    but I wouldn't recommend it, use the keyword "new" instead of "override" to
    hide the original operator or method and then you could return a different
    type.

    Hiding base class members should be done with caution and great reserve.

    HTH
    --
    Dale Preston
    MCAD C#
    MCSE, MCDBA


    "Tony" wrote:
    [color=blue]
    > I have an operator== overload that compares two items and returns a new
    > class as the result of the comparison (instead of the normal bool)
    >
    > I then get an ambiguous operater compile error when I attempt to check to
    > see if the object is null:
    > "The call is ambiguous between the following methods or properties:
    > 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
    > ==(TestObject, TestObject)"
    >
    > Does anyone have any idea how to correct this problem?
    >
    > Example source follows:
    >
    > Thanks,
    > Tony
    >
    > internal class Condition
    > {
    > }
    >
    > internal class TestObject
    > {
    > public static Condition operator==(Test Object oTestObject, string
    > sValue)
    > {
    > return new Condition(); // a stub for illistration purposes
    > }
    >
    > public static Condition operator!=(Test Object oTestObject, string
    > sValue)
    > {
    > return new Condition(); // a stub for illistration purposes
    > }
    >
    > public static Condition operator==(Test Object oTestObject1, TestObject
    > oTestObject2)
    > {
    > return new Condition(); // a stub for illistration purposes
    > }
    >
    > public static Condition operator!=(Test Object oTestObject1, TestObject
    > oTestObject2)
    > {
    > return new Condition(); // a stub for illistration purposes
    > }
    >
    > public override int GetHashCode()
    > {
    > return base.GetHashCod e();
    > }
    >
    > public override bool Equals(object aObject)
    > {
    > return base.Equals(aOb ject);
    > }
    >
    > }
    >
    > internal class Test
    > {
    > void Testmethod()
    > {
    > TestObject oTestObject = null;
    >
    > if (oTestObject == null) // compile error occurs on this line
    > oTestObject = new TestObject();
    > }
    > }
    >
    >
    >[/color]

    Comment

    • Dale

      #3
      RE: Operator== overload problem

      I just re-read my post... Replace overload with override in all instances :)

      Dale
      --
      Dale Preston
      MCAD C#
      MCSE, MCDBA


      "Dale" wrote:
      [color=blue]
      > Yes. Don't overload == for anything except value types.
      >
      > I don't understand why you would return other than a bool? If you want a
      > method to return an instance of a class based upon the comparison, then write
      > a method that calls the comparison and returns the class instance based on
      > the result but don't overload the comparison.
      >
      > When you do have to overload equivelence in objects (and I recommend doing
      > it more often than not) overload Object.Equals() , but you still have to
      > return a bool. You can't change the return type in an overload. You could,
      > but I wouldn't recommend it, use the keyword "new" instead of "override" to
      > hide the original operator or method and then you could return a different
      > type.
      >
      > Hiding base class members should be done with caution and great reserve.
      >
      > HTH
      > --
      > Dale Preston
      > MCAD C#
      > MCSE, MCDBA
      >
      >
      > "Tony" wrote:
      >[color=green]
      > > I have an operator== overload that compares two items and returns a new
      > > class as the result of the comparison (instead of the normal bool)
      > >
      > > I then get an ambiguous operater compile error when I attempt to check to
      > > see if the object is null:
      > > "The call is ambiguous between the following methods or properties:
      > > 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
      > > ==(TestObject, TestObject)"
      > >
      > > Does anyone have any idea how to correct this problem?
      > >
      > > Example source follows:
      > >
      > > Thanks,
      > > Tony
      > >
      > > internal class Condition
      > > {
      > > }
      > >
      > > internal class TestObject
      > > {
      > > public static Condition operator==(Test Object oTestObject, string
      > > sValue)
      > > {
      > > return new Condition(); // a stub for illistration purposes
      > > }
      > >
      > > public static Condition operator!=(Test Object oTestObject, string
      > > sValue)
      > > {
      > > return new Condition(); // a stub for illistration purposes
      > > }
      > >
      > > public static Condition operator==(Test Object oTestObject1, TestObject
      > > oTestObject2)
      > > {
      > > return new Condition(); // a stub for illistration purposes
      > > }
      > >
      > > public static Condition operator!=(Test Object oTestObject1, TestObject
      > > oTestObject2)
      > > {
      > > return new Condition(); // a stub for illistration purposes
      > > }
      > >
      > > public override int GetHashCode()
      > > {
      > > return base.GetHashCod e();
      > > }
      > >
      > > public override bool Equals(object aObject)
      > > {
      > > return base.Equals(aOb ject);
      > > }
      > >
      > > }
      > >
      > > internal class Test
      > > {
      > > void Testmethod()
      > > {
      > > TestObject oTestObject = null;
      > >
      > > if (oTestObject == null) // compile error occurs on this line
      > > oTestObject = new TestObject();
      > > }
      > > }
      > >
      > >
      > >[/color][/color]

      Comment

      • Tony

        #4
        Re: Operator== overload problem

        I'm doing this because the classes I'm building allow me to generate SQL
        using the C# syntax.
        The details get a bit more technical, but I will give it a try.

        Select.Where( Table1["IdColumn"] == Table2["IdColumn"] & Table1["Name"] ==
        Table2["Name"]);

        The above C# code generates the following SQL:
        "Where Table1.IdColumn = Table2.IdColumn and Table1.Name = Table2.Name"

        Table1["IdColumn"] == Table2["IdColumn"] generates a Condition object
        returned from the Columns operator==:
        Where( ) accepts a Condition.
        You can create a new Condition by: Condition & Condition and so forth.

        The problem is when I need to check the value of my Column class (returned
        from a Table) to see if it's null or for any other reason.

        Column oColumn = oTable1["IdColumn"];

        if (oColumn == null) // generates the ambiguous error

        however, I have found a work around by casting it to object first to do a
        reference compare but it's less that intuitive when you see it:

        if ((object)oColum n == null) //. compiles fine

        Tony

        "Dale" <dale0973@nospa m.nospam> wrote in message
        news:4DAB7B1E-D6DB-4F37-B80E-93700D59161B@mi crosoft.com...[color=blue]
        > Yes. Don't overload == for anything except value types.
        >
        > I don't understand why you would return other than a bool? If you want a
        > method to return an instance of a class based upon the comparison, then
        > write
        > a method that calls the comparison and returns the class instance based on
        > the result but don't overload the comparison.
        >
        > When you do have to overload equivelence in objects (and I recommend doing
        > it more often than not) overload Object.Equals() , but you still have to
        > return a bool. You can't change the return type in an overload. You
        > could,
        > but I wouldn't recommend it, use the keyword "new" instead of "override"
        > to
        > hide the original operator or method and then you could return a different
        > type.
        >
        > Hiding base class members should be done with caution and great reserve.
        >
        > HTH
        > --
        > Dale Preston
        > MCAD C#
        > MCSE, MCDBA
        >
        >
        > "Tony" wrote:
        >[color=green]
        >> I have an operator== overload that compares two items and returns a new
        >> class as the result of the comparison (instead of the normal bool)
        >>
        >> I then get an ambiguous operater compile error when I attempt to check to
        >> see if the object is null:
        >> "The call is ambiguous between the following methods or properties:
        >> 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
        >> ==(TestObject, TestObject)"
        >>
        >> Does anyone have any idea how to correct this problem?
        >>
        >> Example source follows:
        >>
        >> Thanks,
        >> Tony
        >>
        >> internal class Condition
        >> {
        >> }
        >>
        >> internal class TestObject
        >> {
        >> public static Condition operator==(Test Object oTestObject, string
        >> sValue)
        >> {
        >> return new Condition(); // a stub for illistration purposes
        >> }
        >>
        >> public static Condition operator!=(Test Object oTestObject, string
        >> sValue)
        >> {
        >> return new Condition(); // a stub for illistration purposes
        >> }
        >>
        >> public static Condition operator==(Test Object oTestObject1,
        >> TestObject
        >> oTestObject2)
        >> {
        >> return new Condition(); // a stub for illistration purposes
        >> }
        >>
        >> public static Condition operator!=(Test Object oTestObject1,
        >> TestObject
        >> oTestObject2)
        >> {
        >> return new Condition(); // a stub for illistration purposes
        >> }
        >>
        >> public override int GetHashCode()
        >> {
        >> return base.GetHashCod e();
        >> }
        >>
        >> public override bool Equals(object aObject)
        >> {
        >> return base.Equals(aOb ject);
        >> }
        >>
        >> }
        >>
        >> internal class Test
        >> {
        >> void Testmethod()
        >> {
        >> TestObject oTestObject = null;
        >>
        >> if (oTestObject == null) // compile error occurs on this line
        >> oTestObject = new TestObject();
        >> }
        >> }
        >>
        >>
        >>[/color][/color]


        Comment

        • Tony

          #5
          Re: Operator== overload problem

          The problem has nothing to do with returning something other than bool, the
          same happens in that case also:

          internal class Condition
          {

          }

          internal class TestObject
          {

          public static bool operator==(Test Object oTestObject, string sValue)
          {
          return true; // a stub for illistration purposes
          }

          public static bool operator !=(TestObject oTestObject, string sValue)
          {
          return true; // a stub for illistration purposes
          }

          public static bool operator ==(TestObject oTestObject1, TestObject
          oTestObject2)
          {
          return true; // a stub for illistration purposes
          }

          public static bool operator !=(TestObject oTestObject1, TestObject
          oTestObject2)
          {
          return true; // a stub for illistration purposes
          }

          public override int GetHashCode()
          {
          return base.GetHashCod e();
          }

          public override bool Equals(object aObject)
          {
          return base.Equals(aOb ject);
          }

          }


          internal class Test
          {
          void Testmethod()
          {
          TestObject oTestObject = null;

          if (oTestObject == null) // compiler error
          oTestObject = new TestObject();
          }
          }



          "Dale" <dale0973@nospa m.nospam> wrote in message
          news:4DAB7B1E-D6DB-4F37-B80E-93700D59161B@mi crosoft.com...[color=blue]
          > Yes. Don't overload == for anything except value types.
          >
          > I don't understand why you would return other than a bool? If you want a
          > method to return an instance of a class based upon the comparison, then
          > write
          > a method that calls the comparison and returns the class instance based on
          > the result but don't overload the comparison.
          >
          > When you do have to overload equivelence in objects (and I recommend doing
          > it more often than not) overload Object.Equals() , but you still have to
          > return a bool. You can't change the return type in an overload. You
          > could,
          > but I wouldn't recommend it, use the keyword "new" instead of "override"
          > to
          > hide the original operator or method and then you could return a different
          > type.
          >
          > Hiding base class members should be done with caution and great reserve.
          >
          > HTH
          > --
          > Dale Preston
          > MCAD C#
          > MCSE, MCDBA
          >
          >
          > "Tony" wrote:
          >[color=green]
          >> I have an operator== overload that compares two items and returns a new
          >> class as the result of the comparison (instead of the normal bool)
          >>
          >> I then get an ambiguous operater compile error when I attempt to check to
          >> see if the object is null:
          >> "The call is ambiguous between the following methods or properties:
          >> 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
          >> ==(TestObject, TestObject)"
          >>
          >> Does anyone have any idea how to correct this problem?
          >>
          >> Example source follows:
          >>
          >> Thanks,
          >> Tony
          >>
          >> internal class Condition
          >> {
          >> }
          >>
          >> internal class TestObject
          >> {
          >> public static Condition operator==(Test Object oTestObject, string
          >> sValue)
          >> {
          >> return new Condition(); // a stub for illistration purposes
          >> }
          >>
          >> public static Condition operator!=(Test Object oTestObject, string
          >> sValue)
          >> {
          >> return new Condition(); // a stub for illistration purposes
          >> }
          >>
          >> public static Condition operator==(Test Object oTestObject1,
          >> TestObject
          >> oTestObject2)
          >> {
          >> return new Condition(); // a stub for illistration purposes
          >> }
          >>
          >> public static Condition operator!=(Test Object oTestObject1,
          >> TestObject
          >> oTestObject2)
          >> {
          >> return new Condition(); // a stub for illistration purposes
          >> }
          >>
          >> public override int GetHashCode()
          >> {
          >> return base.GetHashCod e();
          >> }
          >>
          >> public override bool Equals(object aObject)
          >> {
          >> return base.Equals(aOb ject);
          >> }
          >>
          >> }
          >>
          >> internal class Test
          >> {
          >> void Testmethod()
          >> {
          >> TestObject oTestObject = null;
          >>
          >> if (oTestObject == null) // compile error occurs on this line
          >> oTestObject = new TestObject();
          >> }
          >> }
          >>
          >>
          >>[/color][/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Operator== overload problem

            Tony,

            In your case, you want to write the code like this:

            if (oTestObject == (TestObject) null) // compile error occurs on this line
            oTestObject = new TestObject();

            The reason you get the ambiguity error is because null doesn't have a
            type.

            What you might want to do is have a static instance of your test object
            which represents null and replace null with that.

            Hope this helps.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Tony" <tonyng2@spacec ommand.net> wrote in message
            news:%23JhMxJiI GHA.604@TK2MSFT NGP14.phx.gbl.. .[color=blue]
            >I have an operator== overload that compares two items and returns a new
            >class as the result of the comparison (instead of the normal bool)
            >
            > I then get an ambiguous operater compile error when I attempt to check to
            > see if the object is null:
            > "The call is ambiguous between the following methods or properties:
            > 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
            > ==(TestObject, TestObject)"
            >
            > Does anyone have any idea how to correct this problem?
            >
            > Example source follows:
            >
            > Thanks,
            > Tony
            >
            > internal class Condition
            > {
            > }
            >
            > internal class TestObject
            > {
            > public static Condition operator==(Test Object oTestObject, string
            > sValue)
            > {
            > return new Condition(); // a stub for illistration purposes
            > }
            >
            > public static Condition operator!=(Test Object oTestObject, string
            > sValue)
            > {
            > return new Condition(); // a stub for illistration purposes
            > }
            >
            > public static Condition operator==(Test Object oTestObject1,
            > TestObject oTestObject2)
            > {
            > return new Condition(); // a stub for illistration purposes
            > }
            >
            > public static Condition operator!=(Test Object oTestObject1,
            > TestObject oTestObject2)
            > {
            > return new Condition(); // a stub for illistration purposes
            > }
            >
            > public override int GetHashCode()
            > {
            > return base.GetHashCod e();
            > }
            >
            > public override bool Equals(object aObject)
            > {
            > return base.Equals(aOb ject);
            > }
            >
            > }
            >
            > internal class Test
            > {
            > void Testmethod()
            > {
            > TestObject oTestObject = null;
            >
            > if (oTestObject == null) // compile error occurs on this line
            > oTestObject = new TestObject();
            > }
            > }
            >
            >[/color]


            Comment

            • Tony

              #7
              Re: Operator== overload problem

              Thanks, but that doesn't work if the operator== returns an object and not
              bool.

              This only thing I have come up with that works is to cast it to (object)
              first to do a reference compare, but it's not very intuitive.

              if ((object)oTestO bject == null)
              oTestObject = new TestObject();

              Tny


              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
              message news:eKnws5jIGH A.1312@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Tony,
              >
              > In your case, you want to write the code like this:
              >
              > if (oTestObject == (TestObject) null) // compile error occurs on this
              > line
              > oTestObject = new TestObject();
              >
              > The reason you get the ambiguity error is because null doesn't have a
              > type.
              >
              > What you might want to do is have a static instance of your test object
              > which represents null and replace null with that.
              >
              > Hope this helps.
              >
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              > "Tony" <tonyng2@spacec ommand.net> wrote in message
              > news:%23JhMxJiI GHA.604@TK2MSFT NGP14.phx.gbl.. .[color=green]
              >>I have an operator== overload that compares two items and returns a new
              >>class as the result of the comparison (instead of the normal bool)
              >>
              >> I then get an ambiguous operater compile error when I attempt to check to
              >> see if the object is null:
              >> "The call is ambiguous between the following methods or properties:
              >> 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
              >> ==(TestObject, TestObject)"
              >>
              >> Does anyone have any idea how to correct this problem?
              >>
              >> Example source follows:
              >>
              >> Thanks,
              >> Tony
              >>
              >> internal class Condition
              >> {
              >> }
              >>
              >> internal class TestObject
              >> {
              >> public static Condition operator==(Test Object oTestObject, string
              >> sValue)
              >> {
              >> return new Condition(); // a stub for illistration purposes
              >> }
              >>
              >> public static Condition operator!=(Test Object oTestObject, string
              >> sValue)
              >> {
              >> return new Condition(); // a stub for illistration purposes
              >> }
              >>
              >> public static Condition operator==(Test Object oTestObject1,
              >> TestObject oTestObject2)
              >> {
              >> return new Condition(); // a stub for illistration purposes
              >> }
              >>
              >> public static Condition operator!=(Test Object oTestObject1,
              >> TestObject oTestObject2)
              >> {
              >> return new Condition(); // a stub for illistration purposes
              >> }
              >>
              >> public override int GetHashCode()
              >> {
              >> return base.GetHashCod e();
              >> }
              >>
              >> public override bool Equals(object aObject)
              >> {
              >> return base.Equals(aOb ject);
              >> }
              >>
              >> }
              >>
              >> internal class Test
              >> {
              >> void Testmethod()
              >> {
              >> TestObject oTestObject = null;
              >>
              >> if (oTestObject == null) // compile error occurs on this line
              >> oTestObject = new TestObject();
              >> }
              >> }
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Nick Hounsome

                #8
                Re: Operator== overload problem

                "Tony" <tonyng2@spacec ommand.net> wrote in message
                news:%23JhMxJiI GHA.604@TK2MSFT NGP14.phx.gbl.. .[color=blue]
                >I have an operator== overload that compares two items and returns a new
                >class as the result of the comparison (instead of the normal bool)
                >
                > I then get an ambiguous operater compile error when I attempt to check to
                > see if the object is null:
                > "The call is ambiguous between the following methods or properties:
                > 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
                > ==(TestObject, TestObject)"
                >
                > Does anyone have any idea how to correct this problem?
                >
                > Example source follows:
                >
                > Thanks,
                > Tony
                >
                > internal class Condition
                > {
                > }
                >
                > internal class TestObject
                > {
                > public static Condition operator==(Test Object oTestObject, string
                > sValue)
                > {
                > return new Condition(); // a stub for illistration purposes
                > }
                >
                > public static Condition operator!=(Test Object oTestObject, string
                > sValue)
                > {
                > return new Condition(); // a stub for illistration purposes
                > }
                >
                > public static Condition operator==(Test Object oTestObject1,
                > TestObject oTestObject2)
                > {
                > return new Condition(); // a stub for illistration purposes
                > }
                >
                > public static Condition operator!=(Test Object oTestObject1,
                > TestObject oTestObject2)
                > {
                > return new Condition(); // a stub for illistration purposes
                > }
                >
                > public override int GetHashCode()
                > {
                > return base.GetHashCod e();
                > }
                >
                > public override bool Equals(object aObject)
                > {
                > return base.Equals(aOb ject);
                > }
                >
                > }
                >
                > internal class Test
                > {
                > void Testmethod()
                > {
                > TestObject oTestObject = null;
                >
                > if (oTestObject == null) // compile error occurs on this line
                > oTestObject = new TestObject();
                > }
                > }[/color]

                There are 2 problems.

                The error is because the compiler cannot tell whether null is supposed to be
                a TestObject or a string.
                [ essentially the call is TestObject.oper ator==(oTestObj ect,null) ]

                The major error is that Equals() and ==() can return different values which
                is grossly counter intuitive.

                The "standard" interpretation of Equals() is that 2 things are NEVER equal
                if they are of different types (except possibly stuff link equivalent
                numeric valus). A TestObject is not a kind of string so
                oTestObject.Equ als("hello")
                should return false (or your Condition equivalent) and consistency then
                demands that
                oTestObject == "hello"
                should also always return false which in turn makes it unnecessary.


                Comment

                • Frans Bouma [C# MVP]

                  #9
                  Re: Operator== overload problem

                  Tony wrote:
                  [color=blue]
                  > I have an operator== overload that compares two items and returns a
                  > new class as the result of the comparison (instead of the normal bool)
                  >
                  > I then get an ambiguous operater compile error when I attempt to
                  > check to see if the object is null:
                  > "The call is ambiguous between the following methods or properties:
                  > 'TestObject.ope rator ==(TestObject, string)' and 'TestObject.ope rator
                  > ==(TestObject, TestObject)"[/color]

                  this is logical, as it matches with both. I had the same prob, and
                  solved it as below.

                  What you should do is this:
                  instead of creating overloads for string AND TestObject, make 1
                  method, with type 'object'.

                  Internally, test for the type of object or null. This way you can
                  avoid the compilation error and IF you ever need an overload, you can
                  then also use in your code:

                  testobject==DBN ull.Value

                  which represents testing for null (for example, don't know where you
                  use it for).

                  FB

                  [color=blue]
                  > internal class Condition
                  > {
                  > }
                  >
                  > internal class TestObject
                  > {
                  > public static Condition operator==(Test Object oTestObject,
                  > string sValue)
                  > {
                  > return new Condition(); // a stub for illistration purposes
                  > }
                  >
                  > public static Condition operator!=(Test Object oTestObject,
                  > string sValue)
                  > {
                  > return new Condition(); // a stub for illistration purposes
                  > }
                  >
                  > public static Condition operator==(Test Object oTestObject1,
                  > TestObject oTestObject2)
                  > {
                  > return new Condition(); // a stub for illistration purposes
                  > }
                  >
                  > public static Condition operator!=(Test Object oTestObject1,
                  > TestObject oTestObject2)
                  > {
                  > return new Condition(); // a stub for illistration purposes
                  > }
                  >
                  > public override int GetHashCode()
                  > {
                  > return base.GetHashCod e();
                  > }
                  >
                  > public override bool Equals(object aObject)
                  > {
                  > return base.Equals(aOb ject);
                  > }
                  >
                  > }
                  >
                  > internal class Test
                  > {
                  > void Testmethod()
                  > {
                  > TestObject oTestObject = null;
                  >
                  > if (oTestObject == null) // compile error occurs on this
                  > line oTestObject = new TestObject();
                  > }
                  > }[/color]



                  --
                  ------------------------------------------------------------------------
                  Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
                  My .NET blog: http://weblogs.asp.net/fbouma
                  Microsoft MVP (C#)
                  ------------------------------------------------------------------------

                  Comment

                  • Frans Bouma [C# MVP]

                    #10
                    Re: Operator== overload problem

                    Tony wrote:
                    [color=blue]
                    > I'm doing this because the classes I'm building allow me to generate
                    > SQL using the C# syntax.
                    > The details get a bit more technical, but I will give it a try.
                    >
                    > Select.Where( Table1["IdColumn"] == Table2["IdColumn"] &
                    > Table1["Name"] == Table2["Name"]);
                    >
                    > The above C# code generates the following SQL:
                    > "Where Table1.IdColumn = Table2.IdColumn and Table1.Name =
                    > Table2.Name"
                    >
                    > Table1["IdColumn"] == Table2["IdColumn"] generates a Condition
                    > object returned from the Columns operator==:
                    > Where( ) accepts a Condition.
                    > You can create a new Condition by: Condition & Condition and so forth.
                    >
                    > The problem is when I need to check the value of my Column class
                    > (returned from a Table) to see if it's null or for any other reason.
                    >
                    > Column oColumn = oTable1["IdColumn"];
                    >
                    > if (oColumn == null) // generates the ambiguous error
                    >
                    > however, I have found a work around by casting it to object first to
                    > do a reference compare but it's less that intuitive when you see it:
                    >
                    > if ((object)oColum n == null) //. compiles fine[/color]

                    if you're using it for SQL predicate production, you should go for a
                    single method with 'object' and compare with DBNull.Value. I do that
                    too in my O/R mapper, so I can do:

                    Predicate filter = (CustomerFields .CompanyName == DBNull.Value);

                    which produces a [Customers].[CompanyName] IS NULL predicate in SQL.

                    FB

                    --
                    ------------------------------------------------------------------------
                    Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
                    My .NET blog: http://weblogs.asp.net/fbouma
                    Microsoft MVP (C#)
                    ------------------------------------------------------------------------

                    Comment

                    Working...