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. 42 1583
* "Rigga" <s@v.c> scripsit: 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".
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
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
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader04.plus.net...
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 <Da***********@discussions.microsoft.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
* "=?Utf-8?B?RGF2aWQgV2lsbGlhbXM=?=" <Da***********@discussions.microsoft.com> scripsit: 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)
///
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2l************@uni-berlin.de... * "=?Utf-8?B?RGF2aWQgV2lsbGlhbXM=?="
<Da***********@discussions.microsoft.com> scripsit: 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) ///
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
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.
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader04.plus.net... 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.
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 <thomasdotspinkatsp@mntlworlddotcom> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Rigga <s@v.c> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
In article <40*********************@ptn-nntp-reader02.plus.net>, s@v.c
says... 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."
--
Patrick Steele
Microsoft .NET MVP http://weblogs.asp.net/psteele
Hi Rigga,
Yes it is an object. (However for the rest nothing to add to Jon's
explanation)
Cor
* "Cor Ligthert" <no**********@planet.nl> scripsit: 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.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
I did place an answer below.
:-)
Cor
* Patrick Steele [MVP] <pa*****@mvps.org> scripsit: 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."
Just to add that this is a common design approach, that has been taken in
VB6 too, for example.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
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
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader04.plus.net... 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.
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
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader02.plus.net... 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.
"Rigga" <s@v.c> wrote in message news:40*********************@ptn-nntp-reader04.plus.net... 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.
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Tom Spink <thomasdotspinkatsp@mntlworlddotcom> wrote: 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.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
This is very true, but does not explain what is happening. The compiler
sees:
Dim x as String = "veg"
Dim y as String = "veg"
and says "hey, it is the same constant." Underneath the hood, some magic
happens. NOTE: This is actually the JIT compiler that does this magic and
not the initial IL compile. Tres kewl!
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
************************************************
Think Outside the Box!
************************************************
"Tom Spink" <thomasdotspinkatsp@mntlworlddotcom> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl... 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 "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:2l************@uni-berlin.de... * "=?Utf-8?B?RGF2aWQgV2lsbGlhbXM=?=" <Da***********@discussions.microsoft.com> scripsit: 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) ///
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
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!
************************************************
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader04.plus.net... 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.
See the app I posted in reponse to your intial question. The test app should
give you an idea of what is happening.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
************************************************
Think Outside the Box!
************************************************
"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader02.plus.net... 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.
"Rigga" <s@v.c> wrote in message news:40*********************@ptn-nntp-reader04.plus.net... 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.
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" <Da***********@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com... 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. HTH -- David Williams, VB.NET MVP
"Rigga" wrote:
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.
> Um, no. You *can't* "change the value" of a string object. You can
Well, you *can* do it, but most likely it'll mess things up in other
calculations on strings in your apps...
-mike
Cowboy (Gregory A. Beamer) [MVP] wrote: This is very true, but does not explain what is happening. The compiler sees:
Dim x as String = "veg" Dim y as String = "veg"
and says "hey, it is the same constant." Underneath the hood, some magic happens. NOTE: This is actually the JIT compiler that does this magic and not the initial IL compile. Tres kewl!
I hope whatever pattern matching algorithm for strings that is used is
efficient.
That means that any new string has to run through such a comparison to
determine whether it exists or not.
Whatever little bit of memory is conserved would seem small compared to the
amount of wasted cpu time in performing this operation.
Herfried K. Wagner [MVP] wrote: * "Rigga" <s@v.c> scripsit: 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".
Ok, so if
string y1 = "abcdefghijklmnopqrstuvwxy"
string y2 = "abcdefghijklmnopqrstuvwxyz"
That means that y1 is created, then a search algorithm does a string search
all the way to 'y' and then says -- opps, gotta create a new object.
Man. Talk about /overhead/
Michael Giagnocavo [MVP] <mg*******@atrevido.net> wrote: Um, no. You *can't* "change the value" of a string object. You can
Well, you *can* do it, but most likely it'll mess things up in other calculations on strings in your apps...
You can't do it in safe, managed code outside mscorlib which doesn't
use reflection - is that better? ;)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
>> Well, you *can* do it, but most likely it'll mess things up in other calculations on strings in your apps...
You can't do it in safe, managed code outside mscorlib which doesn't use reflection - is that better? ;)
Sure, just wanted that to be clear :)
-mike
err. doing this on an intel chip is a 1 (one) assembler instruction! I cant remeber the opcode, years since i used it, but on a Z80 (remember them?) the assembler instruction was CPIR i believe - hardy inefficient!
guy
"Lucky Carl" wrote: Cowboy (Gregory A. Beamer) [MVP] wrote:
This is very true, but does not explain what is happening. The compiler sees:
Dim x as String = "veg" Dim y as String = "veg"
and says "hey, it is the same constant." Underneath the hood, some magic happens. NOTE: This is actually the JIT compiler that does this magic and not the initial IL compile. Tres kewl!
I hope whatever pattern matching algorithm for strings that is used is efficient.
That means that any new string has to run through such a comparison to determine whether it exists or not.
Whatever little bit of memory is conserved would seem small compared to the amount of wasted cpu time in performing this operation.
Your showing your age, the Z80 came out around 1980, thats 24 years ago !
I remember because we used them in science club at school.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
Time flies when you don't know what you're doing
"guy" <gu*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com... err. doing this on an intel chip is a 1 (one) assembler instruction! I
cant remeber the opcode, years since i used it, but on a Z80 (remember
them?) the assembler instruction was CPIR i believe - hardy inefficient! guy
"Lucky Carl" wrote:
Cowboy (Gregory A. Beamer) [MVP] wrote:
This is very true, but does not explain what is happening. The
compiler sees:
Dim x as String = "veg" Dim y as String = "veg"
and says "hey, it is the same constant." Underneath the hood, some
magic happens. NOTE: This is actually the JIT compiler that does this magic
and not the initial IL compile. Tres kewl!
I hope whatever pattern matching algorithm for strings that is used is efficient.
That means that any new string has to run through such a comparison to determine whether it exists or not.
Whatever little bit of memory is conserved would seem small compared to
the amount of wasted cpu time in performing this operation.
aye laddie,
2Mhz solder in the resistors NASCOM, write your own (polling) diskcontrollers and it STILL runs today:-)
"One Handed Man ( OHM - Terry Burns )" wrote: Your showing your age, the Z80 came out around 1980, thats 24 years ago !
I remember because we used them in science club at school. --
OHM ( Terry Burns ) . . . One-Handed-Man . . .
Time flies when you don't know what you're doing
"guy" <gu*@discussions.microsoft.com> wrote in message news:D0**********************************@microsof t.com... err. doing this on an intel chip is a 1 (one) assembler instruction! I cant remeber the opcode, years since i used it, but on a Z80 (remember them?) the assembler instruction was CPIR i believe - hardy inefficient! guy
"Lucky Carl" wrote:
Cowboy (Gregory A. Beamer) [MVP] wrote:
> This is very true, but does not explain what is happening. The compiler > sees: > > Dim x as String = "veg" > Dim y as String = "veg" > > and says "hey, it is the same constant." Underneath the hood, some magic > happens. NOTE: This is actually the JIT compiler that does this magic and > not the initial IL compile. Tres kewl! >
I hope whatever pattern matching algorithm for strings that is used is efficient.
That means that any new string has to run through such a comparison to determine whether it exists or not.
Whatever little bit of memory is conserved would seem small compared to the amount of wasted cpu time in performing this operation.
This thread seems to have gone off on tangents everythere.
The original question was if strings are objects. They are, in fact everything is. However some objects do behave differently, like people responding to discussions in news groups.
what does x1 = x2 mean? You need to (thourougly) read the language specs.
is it x1.ReferenceEquals(x2)
or is it x1.Compare(x2) = 0
also, if its the jit compiler, it does happen at runtime (at least once).
All strings have an internal pointer to their own string. even if those strings happen to contain the same group and length of characters.
"Rigga" wrote: 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.
"Rigga" <s@v.c> wrote in message news:40*********************@ptn-nntp-reader04.plus.net... 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.
<"=?Utf-8?B?TWljaGFlbCBHIFc=?=" <Michael G W@discussions.microsoft.com>> wrote: This thread seems to have gone off on tangents everythere.
The original question was if strings are objects. They are, in fact everything is. However some objects do behave differently, like people responding to discussions in news groups.
what does x1 = x2 mean? You need to (thourougly) read the language specs.
Not sure about VB, but in C# you don't need to read the language specs
particularly thoroughly - you just need to see that String overloads
the equality operator, just as other classes can.
is it x1.ReferenceEquals(x2)
or is it x1.Compare(x2) = 0
also, if its the jit compiler, it does happen at runtime (at least once).
If *what's* the JIT compiler?
All strings have an internal pointer to their own string. even if those strings happen to contain the same group and length of characters.
Could you explain that, please? Strings contain their data directly -
there's no extra level of indirection.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
I know you are talking about VB.net but check this, in VC++ there is a compiler option called /Gf or /GF, that creates single copy of identical strings in the program image and memory during execution, resulting in smaller programs, an optimization called *string pooling*.
I think this optimization is already built in to VB.net compiler and hence you get this kind of behavior in VB apps.
Any body plz correct me if I'm wrong.
Hope that helps.
Abubakar. http://joehacker.blogspot.com
"Rigga" wrote: 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.
I think you are talking about string interning, or the string interning
pool, or just plain string pool. This is automatically the behavior of C#
as well as VB.NET -- for constants. You can also take advantage of it for
any string you manipulate, by using String.Intern(). The disadvantage is
that it can slow string assignments down (due to the overhead of searching
the string pool to see if the string needs to be added or if an existing
reference can be returned). However, in many instances this
often-overlooked technique can save tremendous amounts of memory. Many
tables of string values have a lot of repetition.
--Bob
"Abubakar" <Ab******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com... I know you are talking about VB.net but check this, in VC++ there is a
compiler option called /Gf or /GF, that creates single copy of identical
strings in the program image and memory during execution, resulting in
smaller programs, an optimization called *string pooling*. I think this optimization is already built in to VB.net compiler and hence
you get this kind of behavior in VB apps. Any body plz correct me if I'm wrong.
Hope that helps.
Abubakar. http://joehacker.blogspot.com
"Rigga" wrote:
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.
On 2004-07-11, Lucky Carl <ca********@yahoo.no.spam> wrote: Ok, so if
string y1 = "abcdefghijklmnopqrstuvwxy" string y2 = "abcdefghijklmnopqrstuvwxyz"
That means that y1 is created, then a search algorithm does a string search all the way to 'y' and then says -- opps, gotta create a new object.
Man. Talk about /overhead/
Sure, but it's compile-time overhead. Which isn't really a big deal.
On 2004-07-09, Lance Wynn <la********@N.O.S.P.A.M.hotmail.com> wrote: 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.
It really won't.
Dim s as String = "123"
Dim s2 as String = "1234"
s = s & "4"
If s is s2 Then
Console.WriteLine("is")
End If
If s = s2 Then
Console.WriteLine("=")
End If
"David" <df*****@woofix.local.dom> wrote
[Strings] Dim s as String = "123" Dim s2 as String = "1234" s = s & "4"
If s is s2 Then Console.WriteLine("is") End If
If s = s2 Then Console.WriteLine("=") End If
Strings in .NET are weird. Even this code doesn't do what you probably think
it does.
First off, string are immutable - once created, they're not changed. Only
new strings are created. Interning of strings confuses the issues quite a
bit.
A fairly good overview seems to be at: http://www.sliver.com/dotnet/emails/default.aspx?id=6
Richter, in his .NET book, has a pretty good explination of strings as well.
He also gets into the encoding (UTF8/16) issues surrounding strings
including the StringInfo class and all sorts of other goodies.
--
Chris Mullins
>>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.
It really won't.
Dim s as String = "123" Dim s2 as String = "1234" s = s & "4"
If s is s2 Then Console.WriteLine("is") End If
If s = s2 Then Console.WriteLine("=") End If
So the lesson here is don't ever use the '=' Operator with strings.
You must use the Equals method for comparison.
Personally speaking, I would have thought it made more sense that the
string class overrode the '=' operator so that it behaved as the Equals
method (thus making the class behave more like a value class) but there
must have been good reasons to do things the way they did....
--
If you wish to reply to me directly, my addres is spam proofed as:
pbromley at adi dot co dot nz
Or if you prefer - no****@nowhere.com :-)
On 2004-08-03, Peter Bromley <no****@nowhere.com> wrote: 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.
It really won't.
Dim s as String = "123" Dim s2 as String = "1234" s = s & "4"
If s is s2 Then Console.WriteLine("is") End If
If s = s2 Then Console.WriteLine("=") End If
So the lesson here is don't ever use the '=' Operator with strings.
You must use the Equals method for comparison.
Well, that's not the lesson I'd take. I pretty much use '=' exclusively,
which IMHO does exactly what one would presume it does. In general,
you're usually interested in equality, not identity, and I find that
this is especially true with strings.
Personally speaking, I would have thought it made more sense that the string class overrode the '=' operator so that it behaved as the Equals method (thus making the class behave more like a value class) but there must have been good reasons to do things the way they did....
It does behave as the Equals method. In the above example,
s.Equals(s2)
Object.Equals(s, s2)
s = s2
are all true. Only 's is s2' is false.
>> So the lesson here is don't ever use the '=' Operator with strings.
You must use the Equals method for comparison.
Well, that's not the lesson I'd take. I pretty much use '=' exclusively, which IMHO does exactly what one would presume it does. In general, you're usually interested in equality, not identity, and I find that this is especially true with strings.
Well, it's the lesson I painfully learned some months ago :-)Personally speaking, I would have thought it made more sense that the string class overrode the '=' operator so that it behaved as the Equals method (thus making the class behave more like a value class) but there must have been good reasons to do things the way they did....
It does behave as the Equals method. In the above example,
s.Equals(s2) Object.Equals(s, s2) s = s2
are all true. Only 's is s2' is false.
Perhaps there is some difference between VB and C++ but I was
conclusively bitten by my assumption that == and .Equals did the same
thing for Strings.
If you look at the il for the following (C++) code
System::String* s = S"123";
System::String* s2 = S"1234";
s = System::String::Concat(s, S"4");
bool equal = s == s2;
equal = s->Equals(s2);
The == test compiles to "ceq" on the pointers s1 and s2 and not to a
call to op_Equality as documented in MSDN. Perhaps this is a bug....
I'm curious, what does your VB code compile to for the s = s2 example?
--
If you wish to reply to me directly, my addres is spam proofed as:
pbromley at adi dot co dot nz
Or if you prefer - no****@nowhere.com :-)
Peter Bromley <no****@nowhere.com> wrote: Perhaps there is some difference between VB and C++ but I was conclusively bitten by my assumption that == and .Equals did the same thing for Strings.
If you look at the il for the following (C++) code System::String* s = S"123"; System::String* s2 = S"1234"; s = System::String::Concat(s, S"4"); bool equal = s == s2; equal = s->Equals(s2);
The == test compiles to "ceq" on the pointers s1 and s2 and not to a call to op_Equality as documented in MSDN. Perhaps this is a bug....
I'm afraid I don't know whether MC++ is meant to use the overloaded ==
operator in the same way that C# does. (Using == in the C# version of
the above would be fine.)
I'm curious, what does your VB code compile to for the s = s2 example?
It compiles to a call to
Microsoft.VisualBasic.CompilerServices.StringType. Strcmp(s, s2, false).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
On 2004-08-04, Peter Bromley <no****@nowhere.com> wrote: It does behave as the Equals method. In the above example,
s.Equals(s2) Object.Equals(s, s2) s = s2
are all true. Only 's is s2' is false.
Perhaps there is some difference between VB and C++ but I was conclusively bitten by my assumption that == and .Equals did the same thing for Strings.
The difference is that the vb '=' and the c++/c# '==' are not the same
operators. in VB, the '=' operator compares strings for equality.
Technically, there are some differences between it and .Equals, but
for the most part they can be treated as if they did the same thing. If you look at the il for the following (C++) code System::String* s = S"123"; System::String* s2 = S"1234"; s = System::String::Concat(s, S"4"); bool equal = s == s2; equal = s->Equals(s2);
The == test compiles to "ceq" on the pointers s1 and s2 and not to a call to op_Equality as documented in MSDN. Perhaps this is a bug....
I'm curious, what does your VB code compile to for the s = s2 example?
As Jon said, it compiles to
Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(s,s2,false)
The 'Is' operator compiles to a ceq instruction.
Jon Skeet [C# MVP] wrote: Perhaps there is some difference between VB and C++ but I was conclusively bitten by my assumption that == and .Equals did the same thing for Strings.
If you look at the il for the following (C++) code System::String* s = S"123"; System::String* s2 = S"1234"; s = System::String::Concat(s, S"4"); bool equal = s == s2; equal = s->Equals(s2);
The == test compiles to "ceq" on the pointers s1 and s2 and not to a call to op_Equality as documented in MSDN. Perhaps this is a bug....
I'm afraid I don't know whether MC++ is meant to use the overloaded == operator in the same way that C# does. (Using == in the C# version of the above would be fine.)
Well, from my reading of MSDN, if a type has an static op_Equality
member defined, then "type == type" should always compile to a call to
that op_Equality member. This has always been the case for value types
(AFAICT) but isn't the case for String. I might do some more research to
see whether this mapping occurs for any heap objects.
Cheers,
--
If you wish to reply to me directly, my addres is spam proofed as:
pbromley at adi dot co dot nz
Or if you prefer - no****@nowhere.com :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Colin Savage |
last post by:
Please could somebody explain what an "atomized string" is? I have been
doing loops in the msdn around XmlNameTable and NameTable but neither
explain what an atomized string is. The example for one...
|
by: Ian Todd |
last post by:
Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char to read in 1000 lines to the array( i...
|
by: Rigga |
last post by:
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
|
by: Paddy |
last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and
followed up on the use of sum to do the flattening.
A solution was:
I would not have thought of using sum in this way. When...
|
by: cman |
last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays. If not, why not? If so, how?
cman
|
by: hstagni |
last post by:
Where can I find a library to created text-based windows applications?
Im looking for a library that can make windows and buttons inside
console.. Many old apps were make like this, i guess
...
|
by: Edward K Ream |
last post by:
Hello all. I'm tracking down memory leaks in my app. To do this I wrote a
script to show the numbers of each different type of object. But it doesn't
show strings! Here is the script:
import...
|
by: InDepth |
last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble
question to the gurus is:
"What have we won with the decision to have string objects immutable? Or did
we won?"
...
|
by: |
last post by:
I am interested in scanning web pages for content of interest, and then
auto-classifying that content. I have tables of metadata that I can use for
the classification, e.g. : "John P. Jones" "Jane...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |