Visual Basic Methods / Functions

In visual basic, Method is a separate code block and that will contain a series of statements to perform particular operations.

 

Generally, in visual basic Methods are useful to improve the code reusability by reducing the code duplication. Suppose, if we have the same functionality to perform in multiple places, then we can create one method with required functionality and use it wherever it is required in the application.

Syntax of Visual Basic Methods

In visual basic, we can create the Methods either by using Sub or Function keywords like as shown below. If we create a method with Sub keyword that will not allow us to return any value. In case, if you want to return any value, then you need to use Function keyword to create the method.

 

<Access_Specifier> Sub Method_Name(<Parameters>)

    ' Statements to Execute

End Sub

 

Or

 

<Access_Specifier> Function Method_Name(<Parameters>) As <Return_Type>

    ' Statements to Execute

    Return return_val

End Function

If you observe the above syntax, we can create a method either by using Sub or Function keyword based on our requirements.

 

Following table lists the parameter details which we specified while creating the methods.

 

ParameterDescription
Access_Specifier It is useful to define an access level either public or private, etc. to allow other classes to access the method. If we didn’t mention any access modifier, then by default it is private.
Method_Name It must be a unique name to identify the method.
Parameters The method parameters are useful to send or receive a data from method and these are enclosed within parentheses and are separated by commas. In case, if no parameters are required for a method then, we need to define a method with empty parentheses.
Return_Type It is useful to specify the type of value the method can return.
In visual basic, both methods and functions are same, there is no difference and these are a just different terms to do the same thing in visual basic.

Following are the examples of defining the different type of methods in visual basic programming language.

 

Public Sub GetUsers()

' Statements to Execute

End Sub

 

Private Sub InsertUserDetails(ByVal name As String, ByVal age As Integer)

' Statements to Execute

End Sub

 

Public Function GetUserDetails(ByVal userid As Integer) As String

' Statements to Execute

 

End Function

If you observe the above examples, we defined different methods with different access modifiers, return types and with different parameters based on our requirements.

 

Now, we will see the complete example of using the methods in visual basic programming language.

Visual Basic Methods Example

Following is the example of using the methods in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim result As String = GetUserDetails("Suresh Dasari", 31)

        Console.WriteLine(result)

        GetDetails()

        Console.ReadLine()

    End Sub

    Public Sub GetDetails()

        Console.WriteLine("Press Enter Key to Exit..")

    End Sub

    Public Function GetUserDetails(ByVal name As String, ByVal age As Integer) As String

        Dim info As String = String.Format("Name: {0}, Age: {1}", name, age)

        Return info

    End Function

End Module

If you observe the above example, we created two methods (GetDetailsGetUserDetails) with / without parameters and performing required operations.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Methods or Functions Example Result

 

This is how we can use the methods in visual basic applications based on our requirements.

Topics Covered