ByRef Verses ByVal

ok, very simply.

If you use a byval variable in a function it goes in and thats it.
eg
Code:
private function MyFunction(byVal strMyName as string) as string

end function

A byref variable actually allows you to retrieve the value of the variable if it changes in the function.
Code:
private function MyFunction(byRef strMyName as string) as string
    strMyname = "Me"
end function

This will change the strMyName variable to "Me", passing the variable back.

I hope Im explaining this properly tonight ,I seem to be making a mare out of simple things...
 
Yeh you got it.

take these examples:
Code:
private function MyFunction(byRef strName as string) as string
    strName = "You"
end function

strMyName ="Me"

MyFunction(strMyName)

console.writeline(strMyName)  ="You"

Code:
private function MyFunction(byVal strName as string) as string
    strName = "You"
end function

strMyName ="Me"

MyFunction(strMyName)

console.writeline(strMyName)  ="Me"
 
Ok thanks for the help; let me just make sure I got this right. If I use ByRef I can pass the value of MyFunction to another variable no matter what the variable
 
Most of the time you use ByVal. Byval passes a copy of the variable while Byref passes the variable into the calling procedure.
So your original variable is most likely toast which is fine if thats your intention.
 
Also Byref is very usefull

if forsay you want a sub to change a large objects properties or an array of large objects like textures or meshs or vertex buffers, making copies of these would be greatly memory extensive and you would need seperate functions for each object you want to change...Example (overly simple but you get the general idea and you can do more complicated things with it)
Code:
public Sub Add1toEach(byref A as integer, Byref B as integer)
     a+=1
     b+=1
end sub

add1toeach(a,b)
or with byval
public Function Add1(byval A as integer) as integer
      return (A+1)
end function
a = Add1(a)
b = Add1(b)
now i know this is a really pointless example but you get the picture.
 
Back
Top