By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Jeff Molby" <JeffMolby@C_mc_st.n_t> schrieb:
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()


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
'UtilityFunction'.

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

Nov 21 '05 #2

P: n/a
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:eT**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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/>

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.