Wrox Programmer Forums
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2004, 07:29 AM
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inherited Shadowing

I'm having problems with the theory of shadowing.

I have found a definition in a Microsoft book that states:-
"If a class with a shadowed or hidden member is inherited, the shadowed or hidden member is not inherited, and the new class will expose the base member."

In other words when you have:-

1) A base class function
2) A child class function
  ( that inherits the base class and shadows it )
3) A child class that inherits the child class
   function in 2)

Any occurance of the 3) child Class will use the
base class implementation, not the child class implementation in 2).

I tried this and found that my project in fact used
the child class implementation in 2).

Can anybody explain why this contradicts the Microsoft book description? Am I doing something wrong with my test?


Example:-

Base Class 1
------------

Public Function setStringA(ByVal strText1 As String) _
                                          As String
        Return strText1 & " Base SetStringA"
End Function


Child Class 2
-------------
Inherits Person

Public Shadows Function SetStringA(ByVal strText1 _
                                   As String) As String
        Return strText1 & " " & " Simple Shadow SetStringA"
End Function


Child Class 3
-------------

Public Class Extra
    Inherits Employee
End Class


In A Form
---------

Private Sub btnShadow_Click(ByVal sender As System.Object,_
                            ByVal e As System.EventArgs) _
                            Handles btnShadow.Click
     Dim myObj As New Extra
     Dim strReturnValue As String
     strReturnValue = myObj.SetStringA("Testing")
     MessageBox.Show(strReturnValue)
End Sub


Clicking the form = messagebox value “Testing Simple Shadow SetStringA" instead of the expected "Testing Base SetStringA".


 
Old September 7th, 2004, 11:02 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

A) The correction to your test:
Your second class is not achieving anything different. The idea is that Shadowing allows you to hide an inherited public member. The following would result in what Microsoft describes:

Public Class Person
    Public Function setStringA(...) As String
        ...
    End Function
End Class

Public Class FirstChildClass
    Inherits Person
    Private Shadows Function setStringA(...) As String
        ...
    End Function
End Class

Public Class SecondChildClass
    Inherits FirstChildClass
End Class

Of course, if you hide the method by shadowing it as Private, then you can't call that method of the child class or the matching base class' method either. As long as it's public, it will be inherited.

B)
<opinion>
The shadow "functionality" of VB.NET throws mud in the face of OOP. Using Shadows to hide a public method of an inherited class throws a monkey wrench into the concept of inheritance and should be avoided. If you have to use shadowing, then the class design should probably be re-thought.
</opinion>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using JScript on page inherited from MasterPage VictorVictor ASP.NET 2.0 Basics 13 August 11th, 2006 11:58 AM
Please...inherited controls are locked leorochab C# 2005 1 May 21st, 2006 03:41 AM
Please...inherited controls locked ??? leorochab C# 0 May 19th, 2006 12:36 PM
Chapter 5 Shadowing Shared Methods ahanshew BOOK: Professional VB 2005 ISBN: 0-7645-7536-8 0 February 23rd, 2006 04:06 PM
Problems with Inherited Forms in VC# mmwaikar VS.NET 2002/2003 2 July 20th, 2005 10:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.