ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

28-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

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!