• Advertisement
Sign in to follow this  

Quick Question

This topic is 4545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


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

Hth
V@T

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement