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
