Function parameter function

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Hi,

I like to know what do you specify in the function parameter (in the
function implementation) if you want the string that you pass in with the
function call to be changed while its in the function and then you get it
back changed as well?

Is it ByRef or just notthing

function prc0(ByRef editablestring as string )


or

function prc0(editablestring as string )
 
A string is handled a little differently. You can not just set the scoping
as ByRef because strings are immutable. Thus when you change the string, the
reference to the new string inside of the function is changed, but the
reference pointer in the calling method is not changed. You need to pass a
stringbuilder rather than a simple string if you want to manipulate the
return values inside of a sub.

The method declaration you provided seems to indicate that you don't need a
function and could do with a simple method call as in:

Sub prc0(ByRef EditableString as StringBuilder)

Alternatiely, could you alter your calls to have a function that returned a
string rather than changes the mathod's parameters as in:

Function prc0(inString as string) as String
return inString & " is altered"
End Function

The calling method then would be:
MyString = prc0(MyString)

Just some food for thought.
Jim Wooley
 
Joe said:
I like to know what do you specify in the function parameter (in the
function implementation) if you want the string that you pass in with the
function call to be changed while its in the function and then you get it
back changed as well?

Is it ByRef or just notthing

function prc0(ByRef editablestring as string )


or

function prc0(editablestring as string )

'ByRef'.

'Prc(a As String)' is equivalent to 'Prc(ByVal a As String)'. By the way,
if your function does not have a return value, declare it as 'Sub' instead
of 'Function'.
 
Jim Wooley said:
A string is handled a little differently. You can not just set the scoping
as ByRef because strings are immutable. Thus when you change the string,
the reference to the new string inside of the function is changed, but the
reference pointer in the calling method is not changed. You need to pass a
stringbuilder rather than a simple string if you want to manipulate the
return values inside of a sub.

Sorry, but that's wrong. You can pass in a string by-reference and get back
a reference to a new string object.
 
Jim said:
A string is handled a little differently. You can not just set the scoping
as ByRef because strings are immutable. Thus when you change the string, the
reference to the new string inside of the function is changed, but the
reference pointer in the calling method is not changed. You need to pass a
stringbuilder rather than a simple string if you want to manipulate the
return values inside of a sub.

Sorry but the followibg works perfectly:

Sub test()
Dim s As String = "original"
prc0(s)
MsgBox(s)
End Sub

Sub prc0(ByRef EditableString As String)
EditableString = "modified"
End Sub

Message box shows "modified".
 
Back
Top