Strings.. Objects or not???

  • Thread starter Thread starter Rigga
  • Start date Start date
R

Rigga

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.
 
* "Rigga said:
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?

Why not? Both string objects are pointing to the same "constant".
 
I believe the String is a true object, but it Overloads the '=' Operator.
so when you write the code x1=x2 the and x1 and x2 are both strings, it will
actually compile the same as x1 is x2. If your using C#, you can overload
operators for your own classes, and make them behave the same way.

Lance



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.
 
David Williams said:
Remember that strings are ValueTypes, not ReferenceTypes.

No they're not. They're reference types. There are any numbers of ways
to verify that, but Type.IsValueType is probably the easiest.
 
* "=?Utf-8?B?RGF2aWQgV2lsbGlhbXM=?= said:
Remember that strings are ValueTypes, not ReferenceTypes.

No! Strings are not value types, they don't inherit from
'System.ValueType':

\\\
Dim s As String = "Hello World"
MsgBox(TypeOf s Is ValueType)
///
 
But let's not forget that they are immutable.... When you change the value
of a string object, the object is destroyed and a brand new one is created.

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?

and assigning that object to the reference variable as

x1 as string = "veg"

points to the same object as

x2 as string = "veg"

"veg" being the object?

actually, it has always been kind of strange hat you do not have to code

x1 as New String to instantiate the object.

So we are saying that string does not behave in the same way as other
objects?

Anyway, now that I know this I'll have to code around it.. thanks all..

Rigga.
 
Hi Tom,

From you I had expected more, the question is (see it yourself) ..... And
now everybody telling that a string object is a reference type or something
like that.

:-)

Cor
 
Tom Spink said:
But let's not forget that they are immutable.... When you change the value
of a string object, the object is destroyed and a brand new one is created.

Um, no. You *can't* "change the value" of a string object. You can
change the value of a variable so that it's a reference to a different
string, but that doesn't necessarily destroy the old string or create a
new one.
 
Rigga said:
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?

and assigning that object to the reference variable as

x1 as string = "veg"

points to the same object as

x2 as string = "veg"

"veg" being the object?

actually, it has always been kind of strange hat you do not have to code

x1 as New String to instantiate the object.

So we are saying that string does not behave in the same way as other
objects?

Anyway, now that I know this I'll have to code around it.. thanks all..

String behaves like other objects for the most part, but string
literals are interned automatically, so that within a single AppDomain,
all references to the same string literal are references to the same
actual string.

One *really* weird bit about string is that if you create a new string
using the char[] constructor, but give it an empty array, it doesn't
actually create a new string at all - it just returns String.Empty.
Very odd indeed.
 
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?

Yes. It's called "String Interning" and the String.Intern method has a
description of what it's for. Basically (from the docs):

"The common language runtime conserves string storage by maintaining a
table, called the intern pool, that contains a single reference to each
unique literal string declared or created programmatically in your
program. Consequently, an instance of a literal string with a particular
value only exists once in the system."
 
Hi Rigga,

Yes it is an object. (However for the rest nothing to add to Jon's
explanation)

Cor
 
* "Cor Ligthert said:
From you I had expected more, the question is (see it yourself) ..... And
now everybody telling that a string object is a reference type or something
like that.

What would you have expected? It's similar to what's going on for all
the other reference types, so telling that string is a reference type
answers the question, IMO.
 
* Patrick Steele said:
Yes. It's called "String Interning" and the String.Intern method has a
description of what it's for. Basically (from the docs):

"The common language runtime conserves string storage by maintaining a
table, called the intern pool, that contains a single reference to each
unique literal string declared or created programmatically in your
program. Consequently, an instance of a literal string with a particular
value only exists once in the system."

Just to add that this is a common design approach, that has been taken in
VB6 too, for example.
 
Rigga,
In addition to the other comments.

Try the following:

Dim x1 As String = "veggie"
Dim x2 As String = "veg"

x1 = x1.Substring(0, 3)

If x1 = x2 Then
Debug.WriteLine("i expect this code to be executed")
End If

If x1 Is x2 Then
Debug.WriteLine("i do not expect this code to be executed")
End If

Both strings contain "veg", however because the second one was "calculated"
instead of being a constant it is a different string object on the heap.

You can use String.Copy to create a new instances of a string.

Dim x1 As String = "veg"
Dim x2 As String = String.Copy(x1)

Note String.Clone returns the same instance...

Hope this helps
Jay
 
Hey. dont ever change your second name to 'Mortis' !

LOL

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Perhaps I should have quoted my "change" word... but MSDN says:

<MSDN>
A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class.
</MSDN>

This is what I was referring to... whoops

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
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.WriteLine("TEST 1")
Console.WriteLine("-------------")

Dim x1 As String = "veg"
Dim x2 As String = "veg"

If x1 = x2 Then
Console.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

Sub Test2()
Console.WriteLine("TEST 2")
Console.WriteLine("-------------")

Dim x1 As String = veg
Dim x2 As String = veg

If x1 = x2 Then
Console.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

Sub Test3()
Console.WriteLine("TEST 3")
Console.WriteLine("-------------")

Dim sb As New Text.StringBuilder

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.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

End Module

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
Strings are reference types (objects) and not value types (structs). The
issue here lies completely in the compiler, not in the code. Test app:

Module Module1

Private veg As String = "veg"

Sub Main()
Test1()
Test2()
Test3()

Console.Read()
End Sub

Sub Test1()
Console.WriteLine("TEST 1")
Console.WriteLine("-------------")

Dim x1 As String = "veg"
Dim x2 As String = "veg"

If x1 = x2 Then
Console.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

Sub Test2()
Console.WriteLine("TEST 2")
Console.WriteLine("-------------")

Dim x1 As String = veg
Dim x2 As String = veg

If x1 = x2 Then
Console.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

Sub Test3()
Console.WriteLine("TEST 3")
Console.WriteLine("-------------")

Dim sb As New Text.StringBuilder

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.WriteLine("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLine("x1 is x2")
End If

Console.WriteLine("")
End Sub

End Module

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
David Williams said:
Remember that strings are ValueTypes, not ReferenceTypes. Not only that,
but as Herfried stated, due to the way that .NET optimizes strings, both of
those object reference the same IL constant. This is possible because
strings are immutable, i.e. they never change once created.
 
Back
Top