How do you declare a Char literal in Visual Basic

2019-03-17 09:57发布

问题:

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?

回答1:

A character literal is entered using a single character string suffixed with a C.

Dim theLetterA As Char = "A"C


回答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.



回答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("""")