12-05-2012 10:03 AM
If I call a function and pass it a variable, and that function does some calculation with that variable, should the variable in the main function be changed. Let me provide an example below. In this example, the Message box prints 1, 2, and 3. I have always done my code that the only way to get variables FROM a function was explicitly return a value, so for example ReturnVariable=Function(PassedVar). In this case, only ReturnVariable would receive any changes. So for the example below, my assumption is that Msgbox should print 1,1, and 1.
Anyway, this has caused some problems with my programs, but I'm afraid of other issues that I have not found yet. Is there any decleration I can call that will prevent changing of a variable passed to a function? Or is this something that I missed in VB programming, and I need to go through all my code to make sure I don't do this?
Option Explicit 'Forces the explicit declaration of all the variables in a script. Dim MainVar, i MainVar=1 For i = 1 to 3 MsgBox(MainVar) Call Change(MainVar) next Function Change(MyVar) MyVar=MyVar+1 end Function
Solved! Go to Solution.
12-05-2012 01:58 PM
There is a syntax that allows you to influence the behavior
function abc(ByVal param) ' param is copied. Changing param will not influence tha value outside of abc end function function abc(ByRef param) ' The parameter is handled as reference. So changing it in abc will show up outside of abc end function
12-05-2012 02:06 PM
I see now, thanks. Based on some quick experimentation, If I pass multiple parameters, I have to use ByVal for all of them. So, Function ABC(ByVal Param1, ByVal Param2)
Is there any way to default to ByVal instead of ByRef?
12-06-2012 01:25 AM
This is a very currious feature in vbscript
Option Explicit
Sub Increment(param)
param = param + 1
End Sub
Sub IncrementByRef(ByRef param)
param = param + 1
End Sub
Sub IncrementByVal(ByVal param)
param = param + 1
End Sub
Dim Num
dim txt : txt = "Num : " & Num
Num = 0 : IncrementByRef(Num) : txt = txt & VBCRLF & "IncrementByRef(Num) : " & Num
Num = 0 : IncrementByRef Num : txt = txt & VBCRLF & "IncrementByRef Num : " & Num
Num = 0 : IncrementByRef ((Num)) : txt = txt & VBCRLF & "IncrementByRef ((Num)) : " & Num
Num = 0 : call IncrementByRef(Num) : txt = txt & VBCRLF & "call IncrementByRef(Num) : " & Num
Num = 0 : call IncrementByRef ((Num)) : txt = txt & VBCRLF & "call IncrementByRef ((Num)) : " & Num
Num = 0 : IncrementByVal(Num) : txt = txt & VBCRLF & "IncrementByVal(Num) : " & Num
Num = 0 : IncrementByVal Num : txt = txt & VBCRLF & "IncrementByVal Num : " & Num
Num = 0 : IncrementByVal ((Num)) : txt = txt & VBCRLF & "IncrementByVal ((Num)) : " & Num
Num = 0 : call IncrementByVal(Num) : txt = txt & VBCRLF & "call IncrementByVal(Num) : " & Num
Num = 0 : call IncrementByVal ((Num)) : txt = txt & VBCRLF & "call IncrementByVal ((Num)) : " & Num
Num = 0 : Increment(Num) : txt = txt & VBCRLF & "Increment(Num) : " & Num
Num = 0 : Increment Num : txt = txt & VBCRLF & "Increment Num : " & Num
Num = 0 : Increment ((Num)) : txt = txt & VBCRLF & "Increment ((Num)) : " & Num
Num = 0 : call Increment(Num) : txt = txt & VBCRLF & "call Increment(Num) : " & Num
Num = 0 : call Increment ((Num)) : txt = txt & VBCRLF & "call Increment ((Num)) : " & Num
MsgBox txt
So as you see by the output it is a trap. The only secure way is to write ByVal and make sure ByRef works out for you.
Maybe you can get a smile back by reading this blog.
Greetings
Andreas
12-07-2012 08:16 AM
Wow, that is really odd. Thank you for sharing that with me, that blog does help quite a bit in my understanding. I still have a hard time beleiving these are different: "Call Function(Var)" and "Call Function((Var))" but your code certinaly shows that. I will edit my code to use byref and byval.
Is this information in the help file? If not, I think it should be.
Thanks again for sharing. 🙂