Hey all,
If you’re a newbie coder, you’re probably wondering what “overloading” a class is. I wrote this primer up for a long-distance associate who’s cutting his teeth on classes. His name (no surprise) is ”Tom.” ;) Block out about 15 mins and do the below if you’re clueless about overloaded classes.
1.) Create a new web site in Visual Studio.
2.) Right-click on your solution, go to “Add New Item” and create a new class (in should go in the app_code directory). Call your new class “Tom.” It should look just like this:
Imports Microsoft.VisualBasic
Public Class Tom
End Class
So yeah, you got class now.
3.) Now hop over to your webpage’s codebehind (Default.vb) and go into your page_load (accessible via one of the two dropdowns up top) and type “Tom.” Note the period at the end of “Tom.” That’ll give you an intellisense dropdown. Those two auto-generated functions you see are what every new object has by default. So, yes, you JUST created a “Tom” object! Woohoo! Your page_load should look like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Tom.
End Sub
4.) But we don’t want Tom to just have two default functions. That’s lame. Let’s give Tom some hands, feet, and brains functions! Your Tom class should now look like this:
Imports Microsoft.VisualBasic
Public Class Tom
Shared Sub hands()
End Sub
Shared Sub feet()
End Sub
Shared Sub brain()
End Sub
End Class
Save the file then hop back to step 3 again and re-do! You’ll see that when you re-type “Tom.” you get FIVE functions in your dropdown! Tom now has hands, feet, and brains! *sniffle* They grow up so fast…
5.) But Tom’s not finished. Those hands, feet, and brains don’t do shit! Our Tom is weak! Frail! We should add some processing or something in the those Subs so that they actually DO something…but hell with that. You get the idea, you can add processing if you so desire. Let’s move on to overloading!
6.) You can kick off the Tom “hands” function in the page_load just by typing “Tom.hands()” and whatever processing you have in the “hands” Sub will kick off when the page loads. Duh. There’s absolutely no need to send in a variable to the “hands” Sub. “Hands” gets along just fine without variables. BUT let’s say we want to have the option to send “hands” different variables. So we just dupicate “hands” like so:
Imports Microsoft.VisualBasic
Public Class Tom
Shared Sub hands()
End Sub
Shared Sub hands(ByVal ball As String)
End Sub
Shared Sub hands(ByVal ball As String, ByVal bat As String)
End Sub
Shared Sub hands(ByVal ball As String, ByVal bat As String, ByVal glove As String)
End Sub
Shared Sub feet()
End Sub
Shared Sub brain()
End Sub
End Class
Now, go back to the page_load in Default.vb and type “Tom.hands(” After you type the parantheses you’ll see that the “hands” Sub now has FOUR possible ways to call it. You can just kick it off. Or you can send in different variables.
That is “overloading.” Basically, you’re duplicating a Function or Sub in a class BUT changing the variables. That’s all folks. Email if you have any questions. Another way to think of it is to use the phrase: “same function, different variables.”
P.S.—I know what you’re probably thinking: why not just create a Sub with a completely different name if you’re going to be sending in different variables? Why call multiple Subs the same name? This is done primarily for simplicity’s sake because the code in those same-named Subs accomplishes the same thing.