Code: Select all
Procedure change(*u.String)
*u\S ="vladimir was here and so was I!"
EndProcedure
u.String\S="bernard"
a$="Hello!"
Debug u\S
change(@u)
Debug u\S
Debug a$
What is the easiest way to do this? Thanks...
Code: Select all
Procedure change(*u.String)
*u\S ="vladimir was here and so was I!"
EndProcedure
u.String\S="bernard"
a$="Hello!"
Debug u\S
change(@u)
Debug u\S
Debug a$
Yes, that works.mesozorn wrote:I found this in another thread, and it seems to work:
Code: Select all
Procedure change(*u.String) *u\S ="vladimir was here and so was I!" EndProcedure u.String\S="bernard" a$="Hello!" Debug u\S change(@u) Debug u\S Debug a$
I don't think that there is a way to do this.mesozorn wrote:My question is, how can I accomplish this same thing, if I do *NOT* want to use a "structure" as the original string variable, outside the procedure. I just want it to be a normal s.s type string variable, not a s.string structure, where I have to use s\s every time to refer to it.
What is the easiest way to do this? Thanks...
Code: Select all
Procedure.i ByRefString(*buffer, newValue.s)
Protected.i bufLength = MemoryStringLength(*buffer)
If Len(newValue) > bufLength
PokeS(*buffer, Left(newValue,bufLength-1) + "~") ;truncated value
ProcedureReturn 0
EndIf
PokeS(*buffer, newValue, bufLength)
ProcedureReturn 1
EndProcedure
Macro m_ByRefString(txtptr,newValue) ;could also use a macro for this
If Len(newValue) <= MemoryStringLength(txtptr)
PokeS(txtptr, newValue)
Else
PokeS(txtptr, "~") ;truncated value
EndIf
EndMacro
Procedure.s testProc2(*txt)
;this is just an internal procedure that needs a string byref (in addition to the procedure return)
;this is when the checking would be recommended.
If Not ByRefString(*txt,"internal DLL changed")
ProcedureReturn "error"
EndIf
ProcedureReturn "changed"
EndProcedure
Procedure.s testProc3(*txt)
;this is just an internal procedure that needs a string byref (in addition to the procedure return)
;this is when the checking would be recommended.
m_ByRefString(*txt,"internal DLL changed")
ProcedureReturn "changed"
EndProcedure
Define s.s
s=Space(20)
Debug testProc2(@s)
Debug s
s=Space(20)
Debug testProc3(@s)
Debug s
Code: Select all
Procedure zByRef(*A.string, *B.integer) ; .double for double, .long for long, etc.
Debug "Inside: "+PeekS(*A)
Debug PeekI(*B)
cc.s="gerac"
PokeS(*A,Left(PeekS(*A),3)+cc.s) ; change it now
PokeI(*B,PeekI(*B)*10+2)
Debug "Still Inside: "+PeekS(*A)
Debug PeekI(*B)
EndProcedure
V.s="Bernard"
C.i=938
Debug "Before: "+V
Debug C
zByRef(@V,@C)
Debug "After: "+V
Debug C
Code: Select all
Procedure zByRef(*A.STRING) ; .double for double, .long for long, etc.
Debug "Inside: "+PeekS(*A)
PokeS(*A,"ABCDEFGHIJKLMNOPQRSTUVWXYZ") ; change it now
Debug PeekS(*A)
Debug PeekS(*A)
EndProcedure
V.s="Bernard"
zByRef(@V)
Debug "After: "+V
Code: Select all
Procedure zByRef(*ptr.STRING)
Debug "Before = " + *ptr\s
*ptr\s= "Hello matey boy!"
Debug "Inside proc = " + *ptr\s
EndProcedure
Dim a$(0)
a$(0) = "Hello!"
zByRef(a$())
Debug "After = " + a$(0)
Damn! You are completely right Srod! Indeed I also get nonsense results! (PeekS(*A) is getting longer and longer)The problem is that @V is passing the address of the character buffer and not the address of the string variable itself.
Code: Select all
EnableExplicit
Define.s S
Define S2.String
Structure ST
x.i
s.s
y.i
EndStructure
Global ST.ST
Procedure ByRef4StrVar(*txt) ; Pointer to string variable
; Use this method for String variables and
; structures containing string variables.
; Clips string to original length
Protected.i inStrLen = MemoryStringLength(*txt)
Protected.s S
S = "Changed --> " + PeekS(*txt)
If Len(S) > inStrLen
PokeS(*txt, Left(S,inStrLen-1) + "~") ; truncated value
Else
PokeS(*txt, S, inStrLen)
EndIf
EndProcedure
Procedure ByRef4StrArr(*ptr.String)
; Use this method to dynamically change String Array elements
*ptr\s= "A" + Space(128) + "Z"
*ptr\s = ReplaceString(*ptr\s, " ", "~")
EndProcedure
Procedure NestedByRef(*txt)
ByRef4StrVar(*txt)
EndProcedure
Debug "Using ByRef4StrVar:"
S = "S"
Debug "S Before --> " + S
ByRef4StrVar(@S)
Debug "S After --> " + S
Debug "S After --> " + S
Debug "S After --> " + S
Debug ""
Debug "Using ByRef4StrVar:"
st\s = "err"
Debug "ST\S Before --> " + st\s
ByRef4StrVar(@st\s)
Debug "ST\S After --> " + st\s
Debug ""
Debug "Using ByRef4StrArr:"
S2\s = "S2"
Debug "S2\s Before --> " + S2\s
ByRef4StrArr(@S2)
Debug "S2\s After --> " + S2\s
Debug ""
Debug "Using ByRef4StrArr:"
Dim a$(0)
a$(0) = "A"
Debug "A$(0) Before --> " + A$(0)
ByRef4StrArr(@a$())
Debug "A$(0) After --> " + A$(0)
Debug Len(a$(0))
Debug ""
Debug "Using Nested ByRef:"
S = "S"
Debug "S Before --> " + S
NestedByRef(@S)
S = "SS"
NestedByRef(@S)
Debug "S After --> " + S