Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Overloading / Overloading 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 let's take a look at overloading some methods, see what it looks like, see how it works, see how it feels. Let's go out to visual studio and take a look at this. I'm going to start off, go down into my class test here. And I'm going to create a Public Sub Mark and I'm going to do a console right line that says "No arguments". Now obviously to call this, I'm going to create and instantiate an object -Dim aa As New Test() - and then I will call Mark in that will write "no arguments" and so let's build this quickly and go out and take a look at what happened. So I'm going to run class test, and you'll see "no arguments" printed so we called that particular one. Now I'm going to say overloads, I'm going to leave that in there, and I'm going to go down and create a New Mark and I'm going to say - Public Overloads Sub Mark() - and this time I'm going to say - ByVal x As Int32. And I'm going to take console.writeline and here I'm just going to print whatever we pass in. Ok and so now I can go up here and notice I can call aa.Mark, I'm going to pass it an integer. So what you are going to see now is by calling Mark and just by changing what I pass into it will determine which one runs. So I'm going to run both this one and this one, choosing which one runs simply by the type of signature I'm using, or the type of value that I'm passing to it. So I'll run class test, and you notice they both ran, OK let's go back and do one more. Just for fun. Now let's overload one more time, that's for overloading methods. Public Overloads Sub Mark and this time we are going to go - ByVal y As string. And we are going to say console.writeline and go print y, whatever we pass in as y. Now we'll go back up to the top and again we are going to call our object, we are going to call our method and this time we are going to type 'hello'. Now when we run this, let's build it and we'll run our application, you'll notice that it ran all three of those. Now to make this really clear as to what's happening here, let's say that we just type aa.Mark and notice what IntelliSense has given us. If I press my up and down arrows, notice it's telling me that I can execute this method, this overloaded method, either as Mark with no parameters, as Mark with x as an integer or with Mark as with y as a string. So it's actually seeing three there with the same beginning declaration. And so I'm just going to put "this is a string". Now that's all I've told it and what it's going to do is, it's going to look through these different methods from this object, and it's going to find the one that takes the string and run that one. So I build that, I'll go run it, class test and notice it got "this is a string". If I change this to a number, and execute that and run it, you'll notice, it ran that one. So that's overloading. Using the same name with different signatures to handle different functionalities based on how we call that particular method.

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