views:

304

answers:

6

If I needed to escape a double quote character within a string literal I'd use two consecutive double quotes as follows:

Dim name = "Chuck ""Iceman"" Liddell"

However, it doesn't seem like consecutive # works the same way. The compiler is expecting a compiler directive to follow the # character, even when its enclosed in double quotes. How can tell the compiler that I want a # character in my string?

Thanks!

EDIT: as a few of the answers below point out, # is not a reserved character. I closed my solution in Visual Studio and re-opened it and no longer got the compiler squiggles warning me. Thanks!

+6  A: 

# is not a reserved character in a VB.NET string literal.

Dim test = "#" ' no error
Shawn Simon
+1 for you... It looks like you were a bit quicker than me.
David Stratton
A: 
Dim name = @"#cakes" As String

or

Dim name = "\#cakes" As String
Paul Creasey
That's C# strings and VB.Net variable declarations
MarkJ
+3  A: 

You don't need to escape it. The code:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     MessageBox.Show("We're #1")
    End Sub
End Class

works fine as-is

(I took the time to write a whole new Windows app just to be sure on that one.)

David Stratton
+1 for testing ;)
Andrew Rollings
+3  A: 

I am not seeing the behavior you described. The following compiles just fine in Visual Studio 2008

Dim x = "#hello"

What version of Visual Studio are you running and can you give a more complete sample?

JaredPar
+2  A: 

eh? No compiler error here:

Dim s as String = "Hello # there"
Sam
A: 

Are you trying to build a DateTime literal?

Dim d as DateTime = #12/10/2009#
Joel Coehoorn