Procedures Scope And Exception Handling

Sub Procedures and Functions

Procedures are made up of series of Visual Basic statements that, when called, are executed. After the call is finished, control returns to the statement that called the procedure. In this way, procedures make it simple for you to package your code into discrete units. Ideally, each Visual Basic procedure should handle one—and only one—task, to make this easy to remember. You can pass data to procedures and the code in the procedure can work on that data.

There are two types of procedures in Visual Basic .NET: Sub procedures and functions. Sub procedures do not return a value, while functions do.

Sub Procedures

Example

A procedure named DisplayMessage to display a message.

Module Module1
    Sub Main()
    End Sub

    Sub DisplayMessage()
        System.Console.WriteLine("Hello from Visual Basic")
    End Sub
End Module

To execute the code in DisplayMessage, you must call that Sub procedure, which looks like this:

Module Module1
    Sub Main()
        DisplayMessage()
    End Sub

    Sub DisplayMessage()
        System.Console.WriteLine("Hello from Visual Basic")
    End Sub
End Module

Example
The following example passes an argument to the Sub procedure. The keyword ByVal indicates that the text string is passed by value, which means a copy of the string is passed. This is the default in VB .NET.

Module Module1
    Sub Main()
    End Sub

    Sub DisplayMessage(ByVal strText As String)
        System.Console.WriteLine(strText)
    End Sub
End Module

And to pass data to DisplayMessage

Module Module1
    Sub Main()
        DisplayMessage("Hello from Visual Basic")
    End Sub

    Sub DisplayMessage(ByVal strText As String)
        System.Console.WriteLine(strText)
    End Sub
End Module

Functions

Functions return values.

Example

Module Module1
    Sub Main()
    End Sub

    Function Addem(ByVal int1 As Integer, ByVal int2 As Integer) As Long
        Return int1 + int2
    End Function
End Module

There are two ways to set up a function to return a value. The function itself can be declared As type and a value is assigned to the function name.

Function Get_Value() As Integer
  Get_Value = 10
  Exit Function
End Function

The second way to set up a function is to use a Return statement to explicitly pass an identified value back to the calling statement.

Function Get_Value()
  Dim Value As Integer
  Value = 10
  Return Value
End Function

Creating Properties

Properties are used to set configuration data for objects, such as the text in a text box or the width of a list box. Using properties provides you with an interface to set or get the value of data internal to an object. You declare properties using Get and Set procedures in a Property statement.

Examples

Difference Between a Procedure and a Function

A function return values.

Imports System.Console

Module Module1
    Sub Main()
        PrintPi1()
        System.Console.WriteLine("intData2 = " & PrintPi2())
    End Sub

    Sub PrintPi1()
        Dim dblData As Double
        Dim intData As Integer
        dblData = 3.14159
        'intData = CInt(dblData)
        intData = CType(dblData, Integer)
        System.Console.WriteLine("intData1 = " & Str(intData))
    End Sub

    Function PrintPi2() As String
        Dim dblData As Double
        Dim intData As Integer
        dblData = 3.14159
        'intData = CInt(dblData)
        intData = CType(dblData, Integer)
        Return (Str(intData))
    End Function

End Module

Passing a Variable Number of Arguments

To pass a variable number of arguments you need a parameter array. In the following example DisplayMessage is declared. All arguments after the first one go into the parameter array, and you can loop over the parameter array to get all arguments passed

Module Module1
    Sub Main()
        DisplayMessage("First message:", "Hi")
        DisplayMessage("Second message:", "Hello", "there")
        Dim TextArray() As String = {"Hello", "from", "Visual", _
            "Basic"}
        DisplayMessage("Third message:", TextArray)
    End Sub

    Sub DisplayMessage(ByVal Title As String, ByVal ParamArray _
        MessageText() As String)
        Dim intLoopIndex As Integer
        System.Console.WriteLine(Title)
        For intLoopIndex = 0 To UBound(MessageText)
            System.Console.WriteLine(MessageText(intLoopIndex))
        Next intLoopIndex
    End Sub
End Module

Creating Properties

Properties are used to set configuration data for objects. Using properties provides you with an interface to set or get the value of data internal to an object.

Module Module1
    Sub Main()
        Module2.Prop1 = 2
        System.Console.WriteLine("Prop1 = " & Module2.Prop1)
        System.Console.WriteLine("Press Enter to continue...")
    End Sub
End Module

Module Module2
    Private PropertyValue As String
    Public Property Prop1() As String
        Get
            Return PropertyValue
        End Get
        Set(ByVal Value As String)
            PropertyValue = Value
        End Set
    End Property
End Module
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.