Square bracket usage

  • Thread starter Thread starter Daniel Klein
  • Start date Start date
D

Daniel Klein

In the MSDN documentation I see examples like:

Dim val As [String]() = {"a", "b", "c"}

If I remove the brackets, ie, Dim val As String(), it seems to work
the same way.

What are the brackets for? And what is the difference when they are
used/omitted? I can find no mention of this anywhere in the MSDN docs.

Thanks,
Daniel Klein
 
I have created string arrays (or Arrays of any type for that matter), and I
have never used the square brackets.

I think you can use them to create a variable that has the same name as a
keyword in VB.

For example:


Dim [String] As String
 
The use of the square brackets allows you to use reserved words as variable
names. For example.

Dim [String]() As Integer = {1, 2, 3}

MsgBox([String](2).ToString)

However, this is not recommended practice. The practical use of the brackets
are in situations where you have no control over the names being used such
as an SQL statement where the name of a field is a reserved word.

Regards - OHM




* Daniel Klein said:
In the MSDN documentation I see examples like:

Dim val As [String]() = {"a", "b", "c"}

If I remove the brackets, ie, Dim val As String(), it seems to work
the same way.

The brackets are useless in this case.
What are the brackets for? And what is the difference when they are
used/omitted? I can find no mention of this anywhere in the MSDN
docs.

<http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec2_2.asp>
 
* "One Handed Man said:
The use of the square brackets allows you to use reserved words as variable
names. For example.

Dim [String]() As Integer = {1, 2, 3}

MsgBox([String](2).ToString)

However, this is not recommended practice. The practical use of the brackets
are in situations where you have no control over the names being used such
as an SQL statement where the name of a field is a reserved word.

Or:

\\\
Public Property [Next]() As ...
...
End Property
///

\\\
Public Sub [Stop]()
...
End Sub
///
 
Yes, that's another excellent example.

Regards - OHM
* "One Handed Man said:
The use of the square brackets allows you to use reserved words as
variable names. For example.

Dim [String]() As Integer = {1, 2, 3}

MsgBox([String](2).ToString)

However, this is not recommended practice. The practical use of the
brackets are in situations where you have no control over the names
being used such as an SQL statement where the name of a field is a
reserved word.

Or:

\\\
Public Property [Next]() As ...
...
End Property
///

\\\
Public Sub [Stop]()
...
End Sub
///
 
Back
Top