473,698 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strings.. Objects or not???

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 #1
42 1634
* "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/>
Jul 21 '05 #2
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******** *************@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.

Jul 21 '05 #3
David Williams <Da***********@ discussions.mic rosoft.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.IsValueTyp e is probably the easiest.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
* "=?Utf-8?B?RGF2aWQgV2l sbGlhbXM=?=" <Da***********@ discussions.mic rosoft.com> scripsit:
Remember that strings are ValueTypes, not ReferenceTypes.


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

\\\
Dim s As String = "Hello World"
MsgBox(TypeOf s Is ValueType)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Jul 21 '05 #5
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?RGF2aWQgV2l sbGlhbXM=?="

<Da***********@ discussions.mic rosoft.com> scripsit:
Remember that strings are ValueTypes, not ReferenceTypes.


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

\\\
Dim s As String = "Hello World"
MsgBox(TypeOf s Is ValueType)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Jul 21 '05 #6
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.

Jul 21 '05 #7
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
Jul 21 '05 #8
Tom Spink <thomasdotspink atsp@mntlworldd otcom> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3132
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
10
9027
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...
73
2924
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
52
3954
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:
74
3138
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
95
5244
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 | | | ...
1
2018
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:
16
1871
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?
9
2679
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...
0
8676
marktang
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...
0
8608
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,...
0
9029
jinu1996
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...
1
8898
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,...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6524
isladogs
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...
0
4370
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...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.