How do you declare a Char literal in Visual Basic .NET?
44,076
Solution 1
A character literal is entered using a single character string suffixed with a C
.
Dim theLetterA As Char = "A"C
Solution 2
I would use CChar. E.g.:
Dim theLetterA As Char = CChar("A")
Check the MSDN website https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx for details on CChar.
Solution 3
In the case of trying to get a double quote as a character literal, you'll need to use the extra quirky VB format:
Dim theQuote As Char = """"C
Or
Dim theQuote As Char = CChar("""")
Author by
Jason Berkan
I am a programmer, an occasional player of games and an all around geek.
Updated on January 26, 2020Comments
-
Jason Berkan over 4 years
With
Option Strict On
:Dim theLetterA As Char = "A"
returns an error about converting the string
"A"
to aChar
.What is the syntax to enter a
Char
literal? -
Alvaro almost 10 yearsThat really sucks, breaks every convention! Thanks for your help
-
Swanny almost 10 yearsActually literal suffixing to indicate type is as old as C and Basic albeit not for string related types, and I too did feel it was a bit dubious the first time I saw it.
-
Matt over 9 yearsI needed to use """"C to trim quotes from a string. How ugly.
-
RJB almost 9 yearsDeserves more upvotes. A char ctor makes more sense than a string decorator like I've never seen before.
-
sstan over 8 years@RJB: You may like the syntax better, but the fact is that this is not a character literal. It's a statement that performs a runtime conversion from a string to a character. Not the same at all.
-
Kevbo over 8 yearsI agree. this is old VB code and requires the visualbasic reference. The right way is to use the convert class. Convert.ToChar("A")
-
Inspector Squirrel almost 8 yearsThis made me die a little inside.
-
II ARROWS about 7 years@sstan I don't have any official statement, but it's very probable the compiler threats that sequence as character literal ("A"c) on compile time.
-
deed02392 over 6 yearsLowercase 'c' works too. Slightly less ugly. This isn't much different from declaring for example Python strings as unicode literals, is it?
-
ryanwebjackson over 6 yearsVS 2017 (15.5.6) changed the capital C I added to lowercase - I'm assuming this is preferred by Microsoft.
-
ToolmakerSteve over 5 years@Kevbo - that's still a runtime action, instead of a character literal. Probably inefficient. More importantly, much more verbose. How is
Convert.ToChar("A")
a readability improvement over"A"c
? -
andyb over 4 yearsDepends on your definition of readable I suppose. I usually avoid "magic numbers" myself, and although the VB expression is quirky AF, you'd never have to pull up an ASCII table to check it was right.
-
HackSlash over 4 yearsI see. I don't think the ASCII code is "magic" when talking about a CHAR because that is the actual number held by the CHAR. A char is a number. If you were to inspect the memory for
theQuote
you would see34
. -
fcm almost 4 yearsI'm still looking for a better option... VB will die without it.
-
Grant Johnson about 2 yearsHow is this any different or more ugly then float support in C#? const float pi = 3.14159F