Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quick Question

Started by Tomcat2184 Nov 10, 2005 at 12:25 AM 1 replies 500+ views
Original Post
Tomcat2184
Tomcat2184
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 -
Skeleton_V@T
Skeleton_V@T
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
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
erissian
erissian
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.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.