Parameters allow you to pass values to methods. In Visual Basic, it is common convention to always start a parameter off with a lowercase letter. There are two ways to pass parameters: ByVal and ByRef.
Here is an example of how you may pass a string and an Integer to a function which returns a string:
NOTE: Because I did not define the variable "intnum" as byVal or ByRef, Visual Basic will set it to the default, which is ByVal.
To call this function, we can do the following:
In this example, strExample will end with a value of "name4." Next, we will discuss method overloading.
Method overloading is creating multiple methods with the same name, but accepting different types of parameters. One good example of a method that has been overloaded is the ToString() Function. This method can be used on nearly any object, yet they all send different parameters.
This may seem like bad programming. However, there are some scenarios where this can end up being very helpful. For example, if you are accepting a user's input of their age in an input box, the user may enter a number, such as 24. They may also enter a string, such as "Twenty-Four." Using method overloading, you can use the same method name for both of these. Here is an example:
By doing this, I can now send a method named "SetUserAge" either a String or an Integer. Notice that in Visual Basic you should use the term "Overloads" in your method to show that you are overloading that method. Obviously, you cannot overload a method with the same type and number of parameters. By doing this, Visual Basic will not know which method you are referring to.
I hope you have enjoyed this Visual Basic tutorial. If you have any questions about any of this, please do not hesitate to email me at jacob@morrisprogramming.com.
Previous