The compiler recognizes you setting it to the same string and creates IL
that points to a single spot in memory. Run this test as a console
application and it will become crystal clear:
Module Module1
Private veg As String = "veg"
Sub Main()
Test1()
Test2()
Test3()
Console.Read()
End Sub
Sub Test1()
Console.WriteLi ne("TEST 1")
Console.WriteLi ne("-------------")
Dim x1 As String = "veg"
Dim x2 As String = "veg"
If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If
If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If
Console.WriteLi ne("")
End Sub
Sub Test2()
Console.WriteLi ne("TEST 2")
Console.WriteLi ne("-------------")
Dim x1 As String = veg
Dim x2 As String = veg
If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If
If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If
Console.WriteLi ne("")
End Sub
Sub Test3()
Console.WriteLi ne("TEST 3")
Console.WriteLi ne("-------------")
Dim sb As New Text.StringBuil der
sb.Append("v")
sb.Append("e")
sb.Append("g")
Dim x1 As String = "veg"
Dim x2 As String = sb.ToString()
If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If
If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If
Console.WriteLi ne("")
End Sub
End Module
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
*************** *************** *************** ***
Think Outside the Box!
*************** *************** *************** ***
"Rigga" <s@v.c> wrote in message
news:40******** *************@p tn-nntp-reader04.plus.n et...
Hi all,
I am wondering why string's are not true objects?.... Let me explain...
If i write the code
Dim x1 as String = "veg"
Dim x2 as String = "veg"
If x1 = x2 then
' i expect this code to be executed
End If
If x1 is x2 then
' i do not expect this code to be executed
End If
However the second lot of code is executed!
Is this correct behavior??? if so is it true then that strings are not
true objects?
Any thoughts would be appreciated!
Rigga.