# Quick Question

## Recommended Posts

I'm taking a VB .NET course and had a quick question. I have a test tomorrow after noon and wanted to know if anyone can give me a better definition on what the difference in "byVal and byRef" are. My text gives one and i found one online but they aren't very good at explaining. If any can post back b4 my test that would be awesome. Thanks -

##### Share on other sites
Quick answer to you (I won't refer to any 'pointer' definition as it is quite strange in VB):

1.ByVal means the passed variable will be copied then the called funtion will only modify the copy version. Any modification that function has made won't affect the passed variable.
Sub AFunc (ByVal AVar As Integer)    AVar = 5End SubSub Form_Load ()    Dim LocalVar As Integer    LocalVar = 10    AFunc (LocalVar) ;End Sub

After the call, LocalVar is still 10.

2.ByRef means the passed variable is the variable itself, opposite to ByVar, any modification the called funtion has made will remain changed after exiting.
Relacing ByVal with ByRef in my example, the LocalVar will carry 5 after calling AFunc () function.

By default, if I recall exactly, the passed variable without speicified By* will be ByVal.

Hth
V@T

##### Share on other sites
You can think of it like this:

byVal makes the function create a copy of that variable. Whatever you do to it doesn't effect the original variable.

byRef makes the function use the original variable. However you change it inside the function, it stays changed.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627740
• Total Posts
2978884

• 10
• 10
• 21
• 14
• 14