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

  • Thread starter Thread starter Fred Morrison
  • Start date Start date
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?
 
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
 
* "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>
 
Back
Top