Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Visual Basic Methods / Functions
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.
// 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.
Parameter | Description |
---|---|
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.
// 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 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.
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 (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.