Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pasing variables... 1

Status
Not open for further replies.

brellis1

Programmer
May 22, 2003
9
PH
syntactically how do you pass variables by reference to
functions or subroutines in VB
 
I think I'm right in saying that VB, by default, passes arguments by reference. If you wanted an argument to be passed by value then you prefix it with the ByVal keyword.

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 

____________________________________________________
$str = "sleep is good for you. sleep gives you the energy you need to function";
$Nstr = ereg_replace("sleep","coffee",$str); echo $Nstr;

onpnt2.gif
 
Quite correct, Stretchwicster. Now, there's a bit of strangeness about all this when you are working with strings. Normally, when you pass an argument byval, you have two copies of the actual value, whereas if you pass byref, you have two references to the same copy of the actual value. However, If you pass a string argument byval, you are actually passing the value of the pointer to a string. The pointer, of course, is a reference. As such, if you get a byval string argument, you can use the reference that it represents to alter the values of the characters in the string, and have the changes visible to the calling context.

Bob Rodes
 
So can you use pointers in Visual Basic like you can in C++?
 
I better add something to that. VB fixes things internally so that byval and byref work with strings the same way as everything else. But try calling, say, GetWindowsDir from the WinAPI. When you pass a string, you pass byVal a null-terminated string and that string contains the windows directory after getwindowsdir is run.

Bob
 
We've had a recent discussion on on the details of strings in thread222-558844
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top