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("""")
Share:
44,076
Jason Berkan
Author by

Jason Berkan

I am a programmer, an occasional player of games and an all around geek.

Updated on January 26, 2020

Comments

  • Jason Berkan
    Jason Berkan over 4 years

    With Option Strict On:

    Dim theLetterA As Char = "A"
    

    returns an error about converting the string "A" to a Char.

    What is the syntax to enter a Char literal?

  • Alvaro
    Alvaro almost 10 years
    That really sucks, breaks every convention! Thanks for your help
  • Swanny
    Swanny almost 10 years
    Actually 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
    Matt over 9 years
    I needed to use """"C to trim quotes from a string. How ugly.
  • RJB
    RJB almost 9 years
    Deserves more upvotes. A char ctor makes more sense than a string decorator like I've never seen before.
  • sstan
    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
    Kevbo over 8 years
    I 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
    Inspector Squirrel almost 8 years
    This made me die a little inside.
  • II ARROWS
    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
    deed02392 over 6 years
    Lowercase 'c' works too. Slightly less ugly. This isn't much different from declaring for example Python strings as unicode literals, is it?
  • ryanwebjackson
    ryanwebjackson over 6 years
    VS 2017 (15.5.6) changed the capital C I added to lowercase - I'm assuming this is preferred by Microsoft.
  • ToolmakerSteve
    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
    andyb over 4 years
    Depends 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
    HackSlash over 4 years
    I 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 see 34.
  • fcm
    fcm almost 4 years
    I'm still looking for a better option... VB will die without it.
  • Grant Johnson
    Grant Johnson about 2 years
    How is this any different or more ugly then float support in C#? const float pi = 3.14159F