By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,702 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,702 IT Pros & Developers. It's quick & easy.

Function parameter function

P: n/a
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 )
Feb 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Feb 8 '06 #2

P: n/a
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" <jo*******@yahoo.com> wrote in message
news:%2****************@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 )

Feb 8 '06 #3

P: n/a
"Joe" <jo*******@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/>

Feb 8 '06 #4

P: n/a
"Jim Wooley" <jw******@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/>

Feb 8 '06 #5

P: n/a
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
Feb 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.