Inheritance problem - Base Class method being called when Derived Class method should be called.

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

    Inheritance problem - Base Class method being called when Derived Class method should be called.

    This is blowing my mind. For some reason, A.Save() always calls A.Update().
    Shouldn't it call B.Update() when the object is instantiated as a B? I've
    been working with these classes for some time now, override methods without
    any sort of problem, but for some reason this one just isn't working right.
    Does it have something to do with the way I'm using the interface? Thanks
    for any and all help!

    Jeff



    Interface Z
    Sub Save()
    End Interface


    Class A
    Implements Z

    Public Sub Save() Iimplements Z.Save
    Update()
    End Sub

    Protected Overridable Sub Update(Optional Byval ZeroRowsOK as boolean =
    false)
    'Does Stuff
    End Sub
    End Class

    Class B
    Inherits A

    Protected Overrides Sub Update(Optional Byval ZeroRowsOK as boolean =
    false)
    'Does different stuff
    End Sub
    End Class


    Class CallingClass

    Public Shared Main()
    Dim x As New B()

    UtilityFunction (x)
    End Sub

    Sub UtilityFunction ( i As Z)
    i.Save()
    End Sub
    End Class


  • Herfried K. Wagner [MVP]

    #2
    Re: Inheritance problem - Base Class method being called when Derived Class method should be called.

    "Jeff Molby" <JeffMolby@C_mc _st.n_t> schrieb:[color=blue]
    > This is blowing my mind. For some reason, A.Save() always calls
    > A.Update(). Shouldn't it call B.Update() when the object is instantiated
    > as a B?
    >[...]
    > Interface Z
    > Sub Save()
    > End Interface
    >
    > Class A
    > Implements Z
    >
    > Public Sub Save() Iimplements Z.Save
    > Update()
    > End Sub
    >
    > Protected Overridable Sub Update(Optional Byval ZeroRowsOK as boolean =
    > false)
    > 'Does Stuff
    > End Sub
    > End Class
    >
    > Class B
    > Inherits A
    >
    > Protected Overrides Sub Update(Optional Byval ZeroRowsOK as boolean =
    > false)
    > 'Does different stuff
    > End Sub
    > End Class
    >
    >
    > Class CallingClass
    >
    > Public Shared Main()
    > Dim x As New B()
    >
    > UtilityFunction (x)
    > End Sub
    >
    > Sub UtilityFunction ( i As Z)
    > i.Save()[/color]

    The line above will call the 'Save' method defined in 'Z', because you are
    passing your instance of 'B' in a variable/parameter of its base class.
    Either change the data type of 'i' to 'B' or cast 'i' to 'B' inside
    'UtilityFunctio n'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Jeff Molby

      #3
      Re: Inheritance problem - Base Class method being called when Derived Class method should be called.

      I'm terribly sorry and humbled. I accidently put the overrides in a sister
      class of B, so it's no wonder they weren't being called! :) That's what I
      get for working on a Sunday.

      SORRY!

      P.S. If anyone has the capability, please destroy this proof of my idiocy.


      "Jeff Molby" <JeffMolby@C_mc _st.n_t> wrote in message
      news:eTwRnmpIFH A.1172@TK2MSFTN GP12.phx.gbl...[color=blue]
      > This is blowing my mind. For some reason, A.Save() always calls
      > A.Update(). Shouldn't it call B.Update() when the object is instantiated
      > as a B? I've been working with these classes for some time now, override
      > methods without any sort of problem, but for some reason this one just
      > isn't working right. Does it have something to do with the way I'm using
      > the interface? Thanks for any and all help!
      >
      > Jeff
      >
      >
      >
      > Interface Z
      > Sub Save()
      > End Interface
      >
      >
      > Class A
      > Implements Z
      >
      > Public Sub Save() Iimplements Z.Save
      > Update()
      > End Sub
      >
      > Protected Overridable Sub Update(Optional Byval ZeroRowsOK as boolean =
      > false)
      > 'Does Stuff
      > End Sub
      > End Class
      >
      > Class B
      > Inherits A
      >
      > Protected Overrides Sub Update(Optional Byval ZeroRowsOK as boolean =
      > false)
      > 'Does different stuff
      > End Sub
      > End Class
      >
      >
      > Class CallingClass
      >
      > Public Shared Main()
      > Dim x As New B()
      >
      > UtilityFunction (x)
      > End Sub
      >
      > Sub UtilityFunction ( i As Z)
      > i.Save()
      > End Sub
      > End Class
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Inheritance problem - Base Class method being called when Derived Class method should be called.

        Errata:

        It seems that I mixed 'A', 'B', and 'Z'. However, the problem has already
        been solved by the OP...

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        Working...