|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Function parameter functionHi,
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 ) 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 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 ) > > "Jim Wooley" <jwool***@bellsouth.net> schrieb: Sorry, but that's wrong. You can pass in a string by-reference and get back >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. 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/> Jim Wooley wrote:
> A string is handled a little differently. You can not just set the scoping Sorry but the followibg works perfectly:> 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. 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
Show quote
Hide quote
"Joe" <johnty***@yahoo.com> schrieb: 'ByRef'.> 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 ) '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/>
Are people using data binding now?
Strongly Typed Key Value Collections within a For Next loop Accessing embedded resources Upgrading VB2003 to 2005 how to do Binding with code sorting inherited BindingList Problem adding new row in Access Table Adding summary to object browser? [URGENT] Images and Thumbnail : pb with rendering quality How to embed the excel worksheet into my forms? |
|||||||||||||||||||||||