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.
Jul 21 '05
42 1648
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.ReferenceEqu als(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******** *************@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.
<"=?Utf-8?B?TWljaGFlbCB HIFc=?=" <Michael G W@discussions.m icrosoft.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.ReferenceEqu als(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.co m> 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******@discu ssions.microsof t.com> wrote in message
news:4C******** *************** ***********@mic rosoft.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********@yah oo.no.spam> wrote: Ok, so if
string y1 = "abcdefghijklmn opqrstuvwxy" string y2 = "abcdefghijklmn opqrstuvwxyz"
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.hotmai l.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.WriteLi ne("is")
End If
If s = s2 Then
Console.WriteLi ne("=")
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.WriteLi ne("is") End If
If s = s2 Then Console.WriteLi ne("=") 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.WriteLi ne("is") End If
If s = s2 Then Console.WriteLi ne("=") 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.WriteLi ne("is") End If
If s = s2 Then Console.WriteLi ne("=") 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 :-) 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 adds a string to two
different NameTables and then compares them and they are considered equal? I
must be having a real THICK day today coz I'm just not getting it.
Colin
|
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 think!!).
But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array but I think
that gives 50...
|
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 I did help(sum)
the docstring was very number-centric: It went further, and precluded
its use on strings:
|
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
____________________________________
| |
| ------------------ |
| | BUTTON | |
| ...
|
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 gc,types
def printDict(d):
keys = d.keys() ; keys.sort()
print '-' * 30
for key in keys:
|
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?"
Ok. It's a broad, and maybe a very silly question to ask, but still.
I mean, what good has it brought to us? What advantages immutable strings
have against mutable ones?
|
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 T. Smith" "Fred Barzowsky"
"Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc.
etc.
I am wondering what the efficient way to do this in code might be. The dumb
and brute-force way would be to loop through the content...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |