Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string comparison 1

Status
Not open for further replies.

vcllvc

Programmer
Jul 12, 2001
136
US
Can someone tell me the differents of the following 3 IF statements, pls.

Thanks in advance.

Dim strA As String = "hello"
Dim strB As String = "hello"

If strA = strB Then
MsgBox("=")
End If
If strA Is strB Then
MsgBox("is")
End If
If System.String.Equals(strA, strB) Then
MsgBox("compare")
End If

Vincent
 
At first this seemed like a very simple question. But now I see why you are asking.

The first and the third one are the same thing, they determine whether two String objects have the same value

The second one "IS"
Compares two object reference variables. Meaning that it checks to see if they both point to the same one in memory.



So I am sure your question is at this point.
"When I run this code why does the "IS" if statement still work?"

If you chage it to this

Dim strA As New Object
Dim strB As New Object
If strA Is strB Then
MsgBox("is")
End If

It will not evaluate to true

and if you change it to use the constructor

Dim strA As New String("hello")
Dim strB As New String("hello")
If strA Is strB Then
MsgBox("is")
End If

It also will not evaluate to true


But this will

Dim strA As String = "hello"
Dim strB As String = "hello"

If strA Is strB Then
MsgBox("is")
End If


so here is your answer:

A .NET application can optimize string management by maintaining an internal pool of string values known as intern pool. If the value being assigned to a string variable coincides with one of the strings already in the intern pool, no additional memory is created and the variable receives the address of the string value in the pool. The compiler is capable of using the intern pool to optimize string initialization, and have two string variables pointing to the same String object in memory, which of course helps saving memory.

Which is why the "IS" operator says that

Dim strA As String = "hello"
Dim strB As String = "hello"

Point to the same object



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
I am glad it helped. It is almost like a trick question. [2thumbsup]

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top