String : value or reference type

zatalian

Member
Joined
Sep 30, 2007
Messages
10
Programming Experience
10+
Hi,

I'm new to vb.net so this might be an easy question, but i'm a little confused.

According to msdn, String should be a reference type, but when i try the following code :

VB.NET:
        Dim s1, s2 As String

        s1 = "this is a test"
        s2 = s1

        System.Console.WriteLine(s2)

        s1.ToUpper()
        System.Console.WriteLine(s2)

my strings act like they are value types. s2 does not point to s1, so the second writeline is not uppercased...

Any logical explanations?
 
String class is a class so it is a reference type, a string is also immutable, it can't be changed.
A String object is called immutable (read-only) because its value cannot be modified once it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.
ToUpper is a function method, so the result you expected is the return value, which you didn't catch.
 
my strings act like they are value types. s2 does not point to s1, so the second writeline is not uppercased...

Any logical explanations?

As John points out, it is not a question of reference or value type, it is simply that Microsoft took the design decision that, once created, a string is not altered in any way. Any operations that look like they "alter" a string will actually always return a brand new string object which is the result of the operation applied to the original
 
Thanks for the fast and clear answers...

I have a related question :
objects of reference types need to be created with the new keyword, but it is possible to declare strings with ="something". Can i do this with all my selfmade classes to? If so, how? And if not, then how come string is a class but can do this...
 
In java the following is perfectly legal:

String s = new String("hello there");


In java, String is called String to identify it as a reference type. Microsoft's choice to build a special case into the language so that you can write String (technically correct) or string (technically incorrect) is one of syntactic convenience, probably because most developers see strings as a very basic building block of an app.

Also, the
Dim s as String = "new string"
notation is a syntactic shortcut, a special case programmed into the compiler to treat strings in this way in code. It is likely this is done because writing:

MessageBOx.Show(New String("Error text"), New String("The caption"))

merely clutters code and offers no sensible benefit.


Given that we use the special characters of " to denote when to start and stop a string, there are no needs for extraneous symbols or characters. THe presence of New String(...) can be inferred.

You have to consider that, when youre programming, youre speaking in a logical way using a very simple grammar. The compiler is programmed to understand that grammar, and it knows that " " denotes string. Without that knowledge, you would need some other way of denoting what, in your code, was a string and what was a coding instruction. That this simple case exists is enough reason to answer your question of "why does it work?" - because it has been programmed to work

-
On the case of can you configure your code to accept shortcuts like this or not? Well, yes, but you must either:
Rewrite the compiler so it understands what to do with any strange symbols it finds in your code
Convert your strange symbols to normal code before you compile
Write a converter for your object, from string, then you can create it with:
Dim myObj as MyObject = "my object"

The first option isnt a good idea, for the same reason that inventing new words in English isnt a good idea; noone will understand what youre talking about. Take txt spk 4 eg; unls u no a tnagr hu cn trnl84u, uzn wyrd symbls wen ur ritn aint a gud idea.
Reading your code, so you programmer your parser to know what this means:
Dim myServer as MyServer = ~u@h=google:8080##T#F£
Doesnt make much sense to us, and its limited to your special case

The second option? Well, replacing those ~u@h=google:8080##T#F£ via script with New Server("u", "www.google.com", 8080, Nothing, True, False, Currency.GBP) means the standard compiler will understand it, other developers will understand it, but its not your custom code any more is it? (So why do it in the first place)

The third option is a fiddle. Providing a way for the compiler to implicitly convert a string to one of your objects isnt creating a new language, it's just string conversion..

So.. no is the short answer to "can i do this for my own objects" - Yes, but you wouldnt, because it's about as sensible as inventing a language to pass notes in class when youre younger is the long answer :)
 
Let me ask a last question about this...

With autocompletion, i get the following popup when writing the word string :

string.jpg


If String is a class, what does the greenish icon mean, and why doesn't the tooltip say that string is a class. The icon right above String (Stopwatch) is used for all the other classes.

The greenish icon can also be seen on structures like Integer, ... but it can't mean "structure" since String is a Class...

I promise, this is the last annoying question.
 
I think that means 'data type' (both language and CLR), for example both Date and DateTime structure have this icon, other structures doesn't, like Color structure or a user defined.
 
Let me ask a last question about this...

With autocompletion, i get the following popup when writing the word string :

string.jpg


If String is a class, what does the greenish icon mean, and why doesn't the tooltip say that string is a class. The icon right above String (Stopwatch) is used for all the other classes.

The greenish icon can also be seen on structures like Integer, ... but it can't mean "structure" since String is a Class...
I think it is more likely a special case built into the IDE, and possibly means "basic building block" - people certainly dont consider fundamentals like String and DateTime to be classes, but in OO languages, that is how they are manifest. Another one to think about.. is that arrays are actually a special case of generic built into the language from the start. To say Dim s() as String, is actually to say Dim s as Array(Of String) - this looks like a generic, but generics only came along in 2.0 - arrays as a kind of generic were in thre from the start, again as a special case. One of my favourite fiddles in java, when serializing, was that for members that couldnt be serialized because they were primitives (int, boolean etc), was just make a one dimensional array of them, length one: Serialize(new int[]{ myIntMember })
Arrays are objects, even arrays of primitives. and String() is some kind of relative of System.Array, but again, the syntax hides this special treatment for sake of simplicity.

I promise, this is the last annoying question.
No annoying questions here, but it does kinda highlight that VB classically wasnt targeted at "professional" (read: persnickety/nit-picking) developers that demanded a high degree of accuracy of logical thought and consistency. Those developers used proper, elite languages like C++

You may be interested to know that C# is at least consistent in its iconage of strings and suchlike. A string looks like every other class (though String and string being alises of each other is something that bugs me)

If youre starting to raise questions such as this, maybe youre moving to a more strict form of logical thought that would make C# a better language for you (bearing in mind of course, that modern VB.NET is far less inconsistent than the horrific languages that loaned their syntax to it, and it is capable of behaving in every way as professional a language as C#.. It's just that the stigma of it being a noddy/toytown language is persisted by the default setup being one that promotes sloppy coding (Option Strict/Explict = OFF) and some other stuff that should have been dumped long ago, like case insensitivity, and also () brackets used for both indexers and method calls)..

Buuuut that's not really a topic for Gen Diss.. Debate Club maybe :)
 
Last edited:
If youre starting to raise questions such as this, maybe youre moving to a more strict form of logical thought that would make C# a better language for you (bearing in mind of course, that modern VB.NET is far less inconsistent than the horrific languages that loaned their syntax to it, ....

I actually come from more strict and thought through languages like java, but a programmer does not always has a choice. Right now, i have to program in vb.net (thank god it's not vb.6 anymore). I am however lobbying to move over to C#.

Anyway, thank you very much. The answers i got here were very clear and although they were rather technical they helped me a lot in my daily practice.

Thanks! Great forums :)
 
I also came from java, and I used to campaign to upgrade the peons at work from VBA to C# but genuinely the jump is just too big. I started to stop letting it bother me though, especially when I found out all .NET languages are identical under the hood. I now write in whatever .NET language pleases me, I can read all the languages and if I'm asked to produce code in a certain syntax, I convert my existing code (note: avoid differentiating members by case alone if you wish to do this).

Also cool, is VStudios ability to step between C# and VB when debugging - I take VB project libraries written by other coders, drop and reference them into my C# solution and work with them.. No rocket science, but it sure looks cool. My outputs used by other developers are in dll/exe form so they never really find out what syntax I coded in.
 
Back
Top