28-ByVal and ByRef

0
301

This Visual Basic Tutorial is all about ByVal and ByRef.
In this section, we will learn how to use ByVal and Byref to Subs and Function arguments.

 

By Value(ByVal) means that you can set a copy of a variable to your Subroutine and it is a default when you’re passing variables over to a function or Sub.

 

Let’s see the example below.

  1. Dim Num1 As Integer
  2.  
  3. Num1 = 10
  4. Incformula(Num1)
  5.  
  6. MessageBox.Show(Num1)

Create a Subroutine that has a ByVal:

  1. Private Sub Incformula(ByVal num1 As Integer)
  2.  
  3. num1= num1+ 1
  4.  
  5. End Sub

Output:

byvalf1

ByRef (By Reference)  means that you are not handing over a copy of the original variable but pointing to the original variable.

Let’s see the example below.

 

  1. Dim Num1 As Integer
  2.  
  3. Num1 = 10
  4. Incformula(Num1)
  5.  
  6. MessageBox.Show(Num1)

Create a Subroutine that has a ByRef:

  1. Private Sub Incformula(ByVal num1 As Integer)
  2.  
  3. num1= num1+ 1
  4.  
  5. End Sub

Output:

byref1

 

 

Readers might read also:

 

 

Facebook Comments
(Visited 29 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here