string reset? why? how?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've inherited code similar to the following with a comment on resetting the
string. Why would anyone want to do this? How could this reset the string?
What does it mean to reset a string?

Public Sub SetTheStringToSomething(ByRef OutStr As String)
OutStr = "123"
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As String
SetTheStringToSomething(s)
Dim sOriginal$ = s
s = sOriginal 'reset the string ' <- why? how?
Debug.WriteLine(s)
Debug.WriteLine(sOriginal)
End Sub
 
RNeely,

To make his program unreadable for his coworkers and to make himself very
interesting.

Cor
 
Ron,
There could be any number of reasons someone would want to reset a
string, which only means putting it back to its original value.
If the string is used as a global variable that must default to a
certain value, but utilized to temporarily hold a new value that is
referenced somewhere else, then you would want to reset it after it is
referenced.
But, as Cor so eloquently pointed out, the example code is total
nonsense. Is this a true representation of the code in question?

Tom
 
Yes, the representation is accurate. Thanks for the sanity check. The
assignment of s = sOriginal seems superfluous because it is superfluous.
Don't know if this code was perhaps cut and pasted from inside a for loop?
Doesn't matter. It is still superfluous.
 
RNEELY said:
I've inherited code similar to the following with a comment on resetting
the
string. Why would anyone want to do this? How could this reset the
string?
What does it mean to reset a string?

Public Sub SetTheStringToSomething(ByRef OutStr As String)
OutStr = "123"
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As String
SetTheStringToSomething(s)
Dim sOriginal$ = s
s = sOriginal 'reset the string ' <- why? how?
Debug.WriteLine(s)
Debug.WriteLine(sOriginal)


's' and 'sOriginal' always contain the same value because 'sOriginal' is set
to the value of 's' and the value gets reassigned to 's'. I suggest to
remove 'sOriginal'.
 
tomb ha scritto:
Ron,
There could be any number of reasons someone would want to reset a
string, which only means putting it back to its original value.
If the string is used as a global variable that must default to a
certain value, but utilized to temporarily hold a new value that is
referenced somewhere else, then you would want to reset it after it is
referenced.

perhaps they meant:

Dim s As String = Nothing
Dim sOriginal = s
SetTheStringToSomething(s)

s = sOriginal 'reset the string ' <- why? how?

Debug.WriteLine(s)
Debug.WriteLine(sOriginal)
 
RNEELY said:
I've inherited code similar to the following with a comment on resetting the
string. Why would anyone want to do this? How could this reset the string?
What does it mean to reset a string?

Public Sub SetTheStringToSomething(ByRef OutStr As String)
OutStr = "123"
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As String
SetTheStringToSomething(s)
Dim sOriginal$ = s
s = sOriginal 'reset the string ' <- why? how?
Debug.WriteLine(s)
Debug.WriteLine(sOriginal)
End Sub

Another possiblity for you.
This may be a hang-over from a [much] earlier VB Type-ing problem.

If the Sub's /original/ declaration were

Public Sub SetTheStringToSomething(ByRef OutStr)

Then (in VB) it would return a Variant /containing/ a String (and in
VB.Net an /Object/ containing a String).

Since sOriginal$ is explicitly Typed as a $tring, this may be an attempt
to /remove/ the Variant wrapper and get the value back in to a proper
String variable.

Regards,
Phill W.
 
Back
Top