Methods in Visual Basic .NET

In this article, I will explain you about Methods in Visual Basic .NET.
  • 10969

In this article, I will explain you about Methods in Visual Basic .NET.

Methods


Methods are simply member procedures built into the class. In Visual Basic .NET there are two types of methods Functions and Sub Procedures. Methods help us to handle code in a simple and organized fashion. Functions return a value, but Sub Procedures does not return any value. Methods are basically a series of statements that are executed when called. Detail explanation of Sub Procedures and Functions are given below: 

Sub Procedures

In Visual Basic .NET Sub Procedures are the statements enclosed by the
Sub and End Sub statements. Statements are executed when we call the  Sub procedure. The statements within it are executed until the matching End Sub is not found. A Sub procedure performs actions but does not return a value. The starting point of the program Sub Main(), it is also a sub procedure. The control is transferred to Sub Main() Sub procedure automatically when application start execution.

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        'sub procedure Main() is called by default
        Show()
        'sub procedure Show() which we are creating
        Read()
    End Sub
 
    Sub Show()
        Write("This is Sub Procedures")
        'executing sub procedure Show()
    End Sub
End Module

The output of above code is given below:

output1.gif

Functions

Functions are just like Sub procedures except that they can return a value. When we perfome some action on data like evaluate data, calculations or to transform data then we use function. We can declare functions like Sub Procedures except that we have to use the Function keyword instead of Sub.

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Write("Sum of two integer is" & " " & Sum())
        'calling the function
        Read()
    End Sub
 
    Public Function Sum() As Integer
        'declaring a function Sum
        Dim A, B As Integer
        'declaring two integers and assigning values to them
        A = 20
        B = 40
        Return (A + B)
        'performing the addition of two integers and returning it's value
    End Function
 
End Module

The output of above code is given below:

 output2.gif

Summary

I hope this article help you to understand Methods in Visual Basic .NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.