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.
In visual basic, we can create the Methods either by using
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.
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.
|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.|
The following are examples of defining the different methods in a visual basic programming language.
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.
Following is the example of using the methods in a visual basic programming language.
If you observe the above example, we created two methods (GetDetails, GetUserDetails) with/without parameters and performing required operations.
We will get the following result when we execute the above visual basic program.
This is how we can use the methods in visual basic applications based on our requirements.