How to Create and Call a User defined Sub-Routine

How to Create a User defined Subroutine (used like as function)

'End sub

Sub clear()

     TextBox1.Text = ""
     TextBox1.Text = "Enter a Number"
     TextBox1.Text = "N/A"
    'TextBox1.Text = String.Empty
    'TextBox1.Text = "00.00"
     .
     .
     .     
End Sub

'Private Sub -----


NB: To create subroutine, sub keyword is used with sub routine name (say sub clear()),
    and any code segment is written inside it of a vb form/page before any Private 
    sub ... area/section or after End Sub area/section or in between End 
    Sub ---- and next Private sub ---- area.

How to Call/Use  a User defined Subroutine in a vb form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles   
   Button1.Click
       
      clear()    
      'Call Clear()
     
End Sub

How to pass values as parameters/arguments in a subroutine using call by value concept

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Addition(5, 2)   'OR
        
        'Dim a, b As Integer
        'a = 5
        'b = 2
        'Addition(a, b)
End Sub

Private Sub Addition(x As Integer, y As Integer)
        MsgBox(x + y)
End Sub

How to pass & swap values as parameters/arguments in a subroutine using call by reference concept

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a As Integer
        Dim b As Integer
        a = 10
        b = 12

        'Display values before swapping
        Result(a, b)

        'Call Swapping method for swap values
        Swapping(a, b)     'OR use   'Call Swapping(a, b)

        'Display values after swapping
        Result(a, b)
 End Sub

 Private Sub Swapping(ByRef x As Integer, ByRef y As Integer)
        Dim temp As Integer
        temp = x
        x = y
        y = temp
 End Sub

 Private Sub Result(ByRef a As Integer, ByVal b As Integer)
    MsgBox("The value is = " + CStr(a) + vbCrLf + "The value is = " + CStr(b))    
   'MsgBox("The value is = " & CStr(a) & vbCrLf & "The value is = " & CStr(b))     
 End Sub

NB : 
Here Function CStr() = to convert the value in String Form.
          and vbCrLf = to show output in next line.

How to Create and Call a User defined Functions

How to Create a User defined Functions

'End sub

Function clear()

     TextBox1.Text = ""
     TextBox1.Text = "Enter a Number"
     TextBox1.Text = "N/A"
    'TextBox1.Text = String.Empty
    'TextBox1.Text = "00.00"
     .
     .
     .     
     Return 0
End Function

'Private Sub -----

'NB: To create function, Function keyword is used with function name (say Function clear()),
    and any customized code segment is written inside it of a vb form/page before any Private 
    sub ... area/section or after End Sub area/section or in between End 
    Sub ---- and just next Private sub ---- area..

How to Call/Use  a User defined Functions in same page where Function is created

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        clear()
End Sub

How to pass values as parameters/arguments in a user defined Function using call by value concept

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a, b, c As Integer
        a = 32
        b = 64

        c = Addition(a, b)
        MsgBox("The addition result is : " + CStr(c))
End Sub

Private Function Addition(ByVal x As Integer, ByVal y As Integer) As Integer
        Dim Output As Integer
        Output = x + y
        Addition = Output    'OR use 
        'Return Output
End Function

 400 total views,  1 views today

Categories: VB .Net

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.