Shadowing / Shadowing a Method
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now that we've looked at overload, let's talk about shadowing. Now in overriding, shadowing is very similar to overloading and I just want to remind you that where overloading dealt with the signature of a sub procedure shadowing deals with the name of it. Now let's go look at this in visual studio. Let's start off with a Public Class called Test. And I'm going to create a Public Sub called Mark, and I'm going to have this one do a console write line of "hello from Test". So that we can see where it's coming from. Pretty straightforward, now let's go create a new Public Class called Test2 and this one inherits from Test. Now you'll notice that if we instantiate an object from Test2 that we see Mark. And of course we are looking at this particular Mark. Ok, so let's come down here and create a Sub in here and say - Public Overloads Mark - and here we are going to take - ByVal x As Int32 - and then we are going to say - console.writeline (x) - and we are going to end our Sub. I never did put Sub in here, ok, and so now notice what's happening here, I'm overloading Mark across Classes. Ok so when I execute this up here, notice if I try to run the methods of my Class, I'm still seeing Mark. And so if I just execute Mark, I'm going to get from Test and so let's go build this and take a look at it. And notice I'm going to get "hello from Test". If I add an integer in there of say 42 and I build that. You'll notice that I got 42, which meant that this particular overloaded method is the one that ran. Ok now this becomes a problem; what if I want to, in this Class right here, use Public Sub Mark? Ok I'm going to get a squiggly line here and this is going to say wait a minute, you can't do this, you are going to have to shadow this thing. So I type shadows - Public Shadows Sub Mark(). And now I can say console.writeline ("hello from Test2"). Want you to notice something here. I got a squiggly here, because what this is telling me is, you know what I want this one to replace this one. And so I shadowed it and it's not having a problem; but this one is seeing an overload and it was trying to overload back to here and so this says that this one must be declared as shadows. And so I'm going to take overloads and do shadows there, and notice now I have shadowed both of them. Now, Shadow is a form of overload and so it can tell the difference here. Now if I got up here and run Mark (42), obviously I'll get this integer but now if I just run Mark with no arguments, you are going to see its going to come from Test2. And so let's go run it and we run Class Test and notice "hello from Test2". Now hope I didn't loose you there. Notice what happened in my base Class I've got a Sub Mark and it writes "hello from Test", in my Test-2 which inherits from Test I'm picking up Mark but down here I'm shadowing Mark. And then, I'm having to shadow Mark here because it puts it into an overload. And so shadows basically overwrites all the functionality for that particular Sub from the base Class. Public Class Test being my base Class, Public Class Test2 being my derived Class. Now if this does not make sense to you, go back through the video a couple of times until you see what's going on. Shadows is the way we both overload and overwrite from the base Class.
Tutorial Information
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 