Char literals in VB.net
I created a char variable in VB.net and needed to assign a single character to it.
Sounds simple doesn't it ?? Try doing it with Option Strict On
By default the following code will execute without problems :
Dim charVariable as Char = "x"
Here we are assigning a string literal to a char variable. VB.net allows this as it performs type checking and casting at run-time. So the string literal "x" is automatically cast to char.
But with Option Strict On , strict type checking is performed at compile time and the code above will be highlighted as erroneous.
Now here comes to weird part, in C# you can write a char literal like so : 'x'
But you cant write a char literal like this in VB because the hyphen is used for comments.
So, as I found out , in order to assign a char literal to a char in VB.net, you need to append a format specifier to the literal like so :
Dim charVariable as Char = "x"c
[Note the small c after the literal.]
This is the answer to the problem !!!