How do you declare a Char literal in Visual Basic .NET?

vb.netLiterals

vb.net Problem Overview


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?

vb.net Solutions


Solution 1 - vb.net

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

Dim theLetterA As Char = "A"C

Solution 2 - vb.net

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 - vb.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason BerkanView Question on Stackoverflow
Solution 1 - vb.netJeff MercadoView Answer on Stackoverflow
Solution 2 - vb.netAlan BarksdaleView Answer on Stackoverflow
Solution 3 - vb.netandybView Answer on Stackoverflow