Comparing a Class with a Base

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

    Comparing a Class with a Base

    If I have a class foo:

    public class foo
    {
    string baseString;

    public foo()
    {
    baseString = "";
    }

    public foo(string s)
    {
    baseString = s;
    }

    public static bool operator==(foo lhs, foo rhs)
    {
    return lhs.baseString == rhs.baseString;
    }
    }

    And a derived class bar:

    public class bar : foo
    {
    int x;

    public bar()
    {
    x = 0;
    }

    public bar (string s, int y) : base(s)
    {
    x = y;
    }

    public static bool operator==(bar lhs, bar rhs)
    {
    // WHAT GOES IN HERE?
    }
    }

    Do I have to do comparisons on both member variables? Or can I somehow
    pass off the string part to foo to compare? What is the syntax?

    Thanks,

    -- Rick
  • Mattias Sjögren

    #2
    Re: Comparing a Class with a Base

    [color=blue]
    > public static bool operator==(bar lhs, bar rhs)
    > {
    > // WHAT GOES IN HERE?
    > }
    >}
    >
    >Do I have to do comparisons on both member variables? Or can I somehow
    >pass off the string part to foo to compare? What is the syntax?[/color]

    You could do

    return (foo)lhs == (foo)rhs && lhs.x == rhs.x;

    if that's what you're looking for.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Guinness Mann

      #3
      Re: Comparing a Class with a Base

      In article <eyLqQqyoDHA.20 12@TK2MSFTNGP12 .phx.gbl>,
      mattias.dont.wa nt.spam@mvps.or g says...[color=blue]
      >[color=green]
      > > public static bool operator==(bar lhs, bar rhs)
      > > {
      > > // WHAT GOES IN HERE?
      > > }[/color]
      > You could do
      >
      > return (foo)lhs == (foo)rhs && lhs.x == rhs.x;
      >[/color]

      That's exactly what I was looking for, Mattias, thank you!

      -- Rick

      Comment

      Working...