Remedial course in OO...

G

Gill Bates

I suppose I need an OO-101 remedial course, or something.

I would have expected, or hoped, that
Console.WriteLine(Bee(d))
would have printed "Derived1"; not "Base1"..

I'm sure this is an easy thing to accomplish?


Module Module1

Public Class Base1
Public ReadOnly Property Result() As String
Get
Return "Base1"
End Get
End Property
End Class


Public Class Derived1
Inherits Base1
Public Shadows ReadOnly Property Result() As String
Get
Return "Derived1"
End Get
End Property
End Class


Public Sub main()
Dim d As New Derived1

Console.WriteLine(Dee(d))
Console.WriteLine(Bee(d))
End Sub


Public Function Dee(ByVal d As Derived1) As String
Return d.Result
End Function


Public Function Bee(ByVal b As Base1) As String
Return b.Result
End Function

End Module
 
A

Andy Becker

Gill Bates said:
I suppose I need an OO-101 remedial course, or something.

I would have expected, or hoped, that
Console.WriteLine(Bee(d))
would have printed "Derived1"; not "Base1"..

I'm sure this is an easy thing to accomplish?

That looks right to me. d is being passed into Bee as type Base1, so the
shadow implementation is never called (or referred to). Option Strict On
can help you spot these scenarios sometimes.

The variable will always need to be of type Derived1 in order for the shadow
to work. The parameter for Bee basically un-does the shadow, and sees b
only as Base1. Do you see it now?

Best Regards,

Andy
 
F

fred

I think you are confusing Overrides with Shadows. If you want Bee(d) to
return "Base1" then must use Overrides not Shadows it. Of course the
original property must be Overridable.

Fred
 
J

Jay B. Harlow [MVP - Outlook]

Gill,
The others have answered the problem.

If you want a good Intro/How To OO book I would recommend Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

Hope this helps
Jay
 
J

Jonathan Allen

Avoid Shadows. It is meant to be used when someone changes a class you are
inheriting from and you don't want your code to break.
 
G

Gill Bates

All excellent advice. Thanks... got it working now with all the
helpful suggestions...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top