contents   index   previous   next



String constants passed as arguments to subroutines

 

In Visual Basic, as in many other languages, you can pass a string constant to a subroutine that expects a string argument. For example, if subroutine MyStrSub expects a single string argument, any of the following calls are valid:

 

Const MYSTRCONST = "My Visual Basic String"
Dim Str as String

 

Str=MYSTRCONST
call MyStrSub(Str)   'This is a valid call
call MyStrSub(MYSTRCONST)   'This is a valid call
call MyStrSub("My Visual Basic String")  'This is also a valid call

 

In the last two cases, if MyStrSub performs any changes in the string argument, these changes are lost when the program leaves the scope of MyStrSub. The reason for this is that Visual Basic creates a temporary BString with a copy of the constant string that is being passed as an argument to a function or subroutine. Upon return from the subroutine, the temporary BString and its content are destroyed.