Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 25th, 2004, 07:29 AM
Registered User
Join Date: Aug 2004
Location: , , .
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?


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")
End Sub

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

Reply With Quote
  #2 (permalink)  
Old September 7th, 2004, 11:02 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

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.

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.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 12:30 PM.

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