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

Remedial course in OO...

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


P: n/a
"Gill Bates" <gi********@hotmail.com> wrote in message
news:8e**************************@posting.google.c om...
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
Nov 20 '05 #2

P: n/a
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
"Gill Bates" <gi********@hotmail.com> wrote in message
news:8e**************************@posting.google.c om...
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

Nov 20 '05 #3

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

"Gill Bates" <gi********@hotmail.com> wrote in message
news:8e**************************@posting.google.c om...
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

Nov 20 '05 #4

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

--
Jonathan Allen
"Gill Bates" <gi********@hotmail.com> wrote in message
news:8e**************************@posting.google.c om...
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

Nov 20 '05 #5

P: n/a
All excellent advice. Thanks... got it working now with all the
helpful suggestions...
"Jonathan Allen" <X@X> wrote in message news:<e9**************@TK2MSFTNGP09.phx.gbl>...
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.

--
Jonathan Allen
"Gill Bates" <gi********@hotmail.com> wrote in message
news:8e**************************@posting.google.c om...
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

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.