string reset? why? how?

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
 
C

Cor Ligthert [MVP]

RNeely,

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

Cor
 
T

tomb

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
 
G

Guest

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.
 
H

Herfried K. Wagner [MVP]

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'.
 
T

tommaso.gastaldi

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)
 
P

Phill W.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top