Home All Groups Group Topic Archive Search About

Function parameter function

Author
8 Feb 2006 5:43 PM
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 )

Author
8 Feb 2006 5:48 PM
Peter Macej
function prc0(ByRef editablestring as string )

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
8 Feb 2006 6:01 PM
Jim Wooley
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

Show quoteHide quote
"Joe" <johnty***@yahoo.com> wrote in message
news:%23mREFdNLGHA.1288@TK2MSFTNGP09.phx.gbl...
> 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 )
>
>
Author
8 Feb 2006 6:13 PM
Herfried K. Wagner [MVP]
"Jim Wooley" <jwool***@bellsouth.net> schrieb:
>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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Feb 2006 6:27 PM
Peter Macej
Jim Wooley  wrote:
> 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".

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
8 Feb 2006 6:06 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Joe" <johnty***@yahoo.com> schrieb:
> 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'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>