Visual Basic Methods / Functions
Posted by Superadmin on November 15 2022 11:01:27

Visual Basic Methods / Functions

 

 

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

 

Generally, visual basic Methods are useful to improve the code reusability by reducing code duplication. Suppose, if we have the same functionality to perform in multiple places, we can create one method with the required functionality and use it wherever 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. If you want to return any value, 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.

 

The following table lists the parameter details we specified while creating the methods.

 

ParameterDescription
Access_Specifier It is useful to define access levels, i.e., public or private, etc., to allow other classes to access the method. If we didn’t mention any access modifier, it is private by default.
Method_Name It must be a unique name to identify the method.
Parameters The method parameters are useful for sending or receiving data from a method, and these are enclosed within parentheses and separated by commas. If no parameters require for a method, 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 the same, there is no difference, and these are just different terms to do the same thing in visual basic.

The following are examples of defining the different methods in a visual basic programming language.

 

Public Sub GetUsers()
  // Statements to Execute
End Sub

Private Sub InsertUserDetails(ByVal name As StringByVal age As Integer)
  // Statements to Execute
End Sub

Public Function GetUserDetails(ByVal userid As IntegerAs String
  // Statements to Execute
End Function

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

 

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

Visual Basic Methods Example

Following is the example of using the methods in a 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 StringByVal age As IntegerAs 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.

 

We will get the following result when we execute the above visual basic program.

 

Visual Basic Methods or Functions Example Result

 

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