Showing posts with label Methods. Show all posts
Showing posts with label Methods. Show all posts

Sunday, February 6, 2011

Method Overloading

What is Method Overloading?
Method Overloading is a means to having multiple methods with the same name, but which have differing parameter lists. Also called 'Polymorphism' - one name, many forms.

eg. getUserName function with empty parameter list
Public Function getUserName() As String
   ' Code...
Return Value
End Function

eg. getUserName function with a single string parameter called UserID
Public Function getUserName(UserID as Integer) As String
   ' Code...
Return Value
End Function

eg. getUserName function with a string parameter called UserID & a Date parameter called MembershipDate
Public Function getUserName(UserID As Integer, MembershipDate As Date) As String
   ' Code...
Return Value
End Function

Further reading:
Create Overloaded Methods in VB.NET http://www.devx.com/dotnet/Article/9303
Method Overloading http://en.wikipedia.org/wiki/Method_overloading

Wednesday, October 27, 2010

Parameters in Subroutines & Functions

Sometimes a behavior will require further information to accomplish a task. When an input is required for a behavior to execute, this input is passed to the procedure as one or a number of parameters (sometimes called arguments).

Code eg: Subprocedure using parameters.

Public Sub Login(Username As String, Password As String)
    ' Code does something
End Function

Code eg: Function using parameters.

Public Function Calculate(NumberOne As Integer, NumberTwo As Integer) As Integer
    ' Code does something
   Return AValue
End Function

Functions

A function returns a value to the procedure that called it.

Code eg 1: Function.

Public Function CalculateScore() as Integer
        ' Declare variables
        Dim TotalDamage As Integer = 555
    Dim TotalAgility As Integer = 6
        ' Create the expression
        Dim TotalPoints As Integer = TotalDamage * TotalAgility
    Return
TotalPoints
End Function

A function returns a value to the calling procedure. In the above case, the function will return the value of the variable called TotalPoints. The As Integer part of the function header, indicate that the function will return a value that is of the Integer type.

Code eg 2: Function.

Public Function getFirstName() as String
    Return strFirstName
End Function

This new example shows a function that will return a String value.

Subroutines

A subroutine simply executes code when called by another procedure.

Code eg: Subroutine.

Public Sub CalculateScore()
        ' Declare variables
        Dim TotalDamage As Integer = 555
        Dim TotalAgility As Integer = 6
        ' Create the expression
        Dim TotalPoints As Integer = TotalDamage * TotalAgility
End Sub

A subroutine does not return a value to the calling procedure.

If you need to return a value - use a function.