Dim x as [String] vs. Dim x As String - why?

F

Fred Morrison

I've seen numerous examples of

Dim x as [String]

but can't for the life of me find anything in online help in VS 2003 or the
dozen or so .Net books I have. Is this anything like the old Dim x$
technique from a long time ago or is it something completely new to Vb.Net?
 
J

Jay B. Harlow [MVP - Outlook]

Fred,
Square brackets [ ] allow a keyword to be used as an identifier. Seeing as
String is a VB.NET alias for System.String, using [String] or String to
represent System.String are all identical.

Where you really need it is for Enum & Delegate, as both are keywords used
to define new types, so if you wanted use the System.Enum or System.Delegate
type you either need to qualify them with System or escape them

Dim values() as String = [Enum].GetValues(GetType(MyEnum))

Escaped identifiers are also useful when you want to use a keyword for a
method name.

Public Class Car

Public Sub Start()
End Sub

Public Sub [Stop]()
End Sub

End Class

Without an escaped identifier you would need to have the Car.Stop method
called something else, which may not be as obvious as to what the method
does...

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* "Fred Morrison said:
I've seen numerous examples of

Dim x as [String]

but can't for the life of me find anything in online help in VS 2003 or the
dozen or so .Net books I have. Is this anything like the old Dim x$
technique from a long time ago or is it something completely new to Vb.Net?

In addition to Jay's explanation, the link to the documentation for "[",
"]":

<http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec2_2.asp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top