Sign in to follow this  

Quick Question

Recommended Posts

Tomcat2184    122
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 this post

Link to post
Share on other sites
Skeleton_V@T    512
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 = 5
End Sub

Sub 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.


Share this post

Link to post
Share on other sites
erissian    727
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this