|
-
Aug 27th, 2012, 02:59 AM
#1
Thread Starter
Frenzied Member
byval usage
Hi all, I understand what byval and byref as arguments are, but I can see myself us a lot of byref, but not a single byval - can someone points to me "when" you actually use byval with an example? If you just tell me "when you don't want to change the variable's value" that's not going to help me.
-
Aug 27th, 2012, 03:29 AM
#2
Addicted Member
Re: byval usage
you know whats funny ByRef is actually used by default. So by using ByRef as a argument in a function you are pretty much not changing anything it's the same as not using anything.
ByVal is used usually when you want to modify data directly without modifying the data that's passed as a argument.
Say you code like a buffer that holds some data.
Code:
Sub Test()
Dim buffer(4) as byte
buffer(0) = 0
buffer(1) = 1
buffer(2) = 2
buffer(3) = 3
buffer(4) = 4
Call ChangeBuffer(buffer)
Debug.Print buffer(0) & ", " & buffer(1) & ", " & buffer(2) & ", " & buffer(3) & ", " & buffer(4)
'You'll see the outputs will be 1 3 3 3 7 instead of 0 1 2 3 4
'Also try this one
Call ChangeBufferTwo(buffer)
Debug.Print buffer(0) & ", " & buffer(1) & ", " & buffer(2) & ", " & buffer(3) & ", " & buffer(4)
'You'll see the outputs will be 5 5 5 5 6 instead of 1 3 3 3 7 (which is from top change)
'So you see without ByRef it still works same way.
Call ChangeBufferThree(buffer)
Debug.Print buffer(0) & ", " & buffer(1) & ", " & buffer(2) & ", " & buffer(3) & ", " & buffer(4)
'You'll see the outputs will be 1 3 3 3 7 instead of the 4 4 4 4 4
'So you see without ByRef it still works same way.
'You can use ByVal in this case
dim newbuffer() as byte
newbuffer = ChangeBufferFour(buffer)
'You will see buffer will not change, so your original data doesn't get altered but you still altered it and now its in a different buffer.
Debug.Print buffer(0) & ", " & buffer(1) & ", " & buffer(2) & ", " & buffer(3) & ", " & buffer(4)
'You will see buffer is still 1 3 3 3 7 it was before
Debug.Print newbuffer(0) & ", " & newbuffer(1) & ", " & newbuffer(2) & ", " & newbuffer(3) & ", " & newbuffer(4)
'But now this newbuffer has new values 6 6 6 6 6
End Sub
Sub ChangeBuffer(ByRef buffer() as byte)
buffer(0) = 1
buffer(1) = 3
buffer(2) = 3
buffer(3) = 3
buffer(4) = 7
End Sub
Sub ChangeBufferTwo(buffer() as byte)
buffer(0) = 5
buffer(1) = 5
buffer(2) = 5
buffer(3) = 5
buffer(4) = 6
End Sub
Sub ChangeBufferThree(ByVal buffer() as byte)
buffer(0) = 4
buffer(1) = 4
buffer(2) = 4
buffer(3) = 4
buffer(4) = 4
End Sub
Function ChangeBufferFour(ByVal buffer() as byte) as byte()
buffer(0) = 6
buffer(1) = 6
buffer(2) = 6
buffer(3) = 6
buffer(4) = 6
ChangeBufferFour = buffer
End Function
-
Aug 27th, 2012, 03:36 AM
#3
Re: byval usage
 Originally Posted by vbbit
If you just tell me "when you don't want to change the variable's value" that's not going to help me. 
Well, that's the main reason for using it !
There's one very important time when you must use ByVal and that's when passing String Types to Windows API Functions that expect a String. Failing to use ByVal in these circumstances will, at best give incorrect results and at worst crash your program (and / or the IDE). Also, if an API expects a 'Null' Long value it should be passed 'ByVal 0&'
-
Aug 27th, 2012, 06:37 AM
#4
Re: byval usage
There are very few cases where you must use ByVal (such as the ones Doogle mentioned), but a very large amount where you should.
The vast majority of Subs/Functions/etc have absolutely no need to modify the variables used in the caller, they only need to work with the values they were given (and in the case of a Function, return a new value of some sort).
If the value of a parameter does not need to be changed in the caller, then as a general rule it should be specified ByVal.
That reduces the chances of bugs, because you then can't accidentally alter the callers variables, which would lead to unpredictable behaviour. It also helps with self-documentation of the code, because you can see just from the parameter list which parameter(s) can be modified.
The Sub/Function/etc itself usually doesn't care whether ByVal or ByRef were used, it generally only alters the behaviour of the caller - and a well written Sub/Function/etc will not affect the caller in any way unless it is explicitly designed to do so.
-
Aug 27th, 2012, 08:48 AM
#5
Re: byval usage
Also keep in mind that if you are passing string data to a sub or function and that string is very large then using byVal will slow things down a bit. In most cases it is a very small amount but in cases where the procedure is called many times with with a string of 100 bytes or more then it starts to become an issue as byVal sends a copy of the data whereas byRef sends a reference to the string. The reference will be the same small size no matter how much data is in the string but the byVal data will be the size of the string. Strings of just 1-8 bytes will be as fast or possibly even faster byVal larger ones will not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|