Why square brackets [ ] in a declaration

Thonord

Member
Joined
Oct 25, 2012
Messages
23
Programming Experience
10+
As in: strServer As [String] as opposed to: strServer As String?
 
If I am not mistaken, it tells the compiler to interpret the word in brackets as a type. In your example it's useless. Lets say you created a type 'Do' (which you shouldn't ever think of doing), if you 'Dim a As New Do', you get an error. If you 'Dim a As New [Do]', you get no error. Pretty sure this is only supported to make upgrading code from a previous framework easier, you shouldn't normally ever have to use it, except in special cases (like calling a shared method from Enum...).
 
The use of brackets in VB code is for exactly the same reason as it is done in SQL code. As suggested, it allows you to use a keyword as an identifier, e.g. a table or column name in SQL and a type or member name in VB.

I've seen a number of code examples that use [String] and I have to say, I can't understand why anyone would do such a thing. What it actually does is tell the compiler to interpret it as the System.String class rather than the VB.NET inbuilt String data type. Notice that the word turns blue without the brackets, indicating that it's a language keyword, and not with the brackets. The thing is, the VB.NET String data type is implemented using the System.String class so it actually makes no difference which you use anyway. The same goes for Byte, Single, Double, Decimal, Boolean and Char, which are also VB.NET data types that are implemented using .NET types of the same name. The VB.NET Date, Short, Integer and Long data types are implemented using the DateTime, Int16, Int32 and Int64 structures, so you'd never need to use brackets in their case.

Where brackets can be genuinely useful is cases where you want to use a keyword as a member or variable name. You should generally avoid them but there are times when they make sense. For instance, let's say that you have an Appointment class and you want to declare a Date property. Date is a keyword in VB.NET so you need to wrap the property name in brackets in order for the code to compile:
Public Property [Date]() As Date
Another example is if you want to work with a range defined by variables named 'start' and 'end'. End is a VB keyword so you must wrap the variable name in brackets to make it compile:
Private Function CheckDateRange(start As Date, [end] As Date) As Boolean
    '...
End Function
In cases like that though, I'd probably recommend using the names 'startDate' and 'endDate', in which case no brackets are required.

By the way, there's actually no such thing as square brackets because all brackets are square. What people often refer to as "round brackets" are actually parentheses, what's referred to as curly brackets are actually braces and what's referred to as angled brackets are actually chevrons. What's referred to as square brackets are just brackets and only they are brackets. I blame the education system.
 
Yes, but they ARE square... lol

Another example for the brackets:

    Enum Colors
        Red
        Blue
        Green
    End Enum

    Dim colorValues = [Enum].GetValues(GetType(Colors))

This doesn't compile without brackets.
 
Yes, but they ARE square... lol
They are indeed, but then balls are round and cars have four wheels.
Another example for the brackets:

    Enum Colors
        Red
        Blue
        Green
    End Enum

    Dim colorValues = [Enum].GetValues(GetType(Colors))

This doesn't compile without brackets.
I didn't think of that and that is one example of a case that you can't possibly avoid, while the others technically could be avoided.
 
Thank you very much gentlemen.

Tom

P.S. Even though I now know their proper designation, I reject them and substitute them with my own: () "Parentes", {} "C#type" and [] boks.
 
Herman said:
Dim colorValues = [Enum].GetValues(GetType(Colors))

This doesn't compile without brackets.
Try this instead, this is qualifying a type and not using a reserved keyword:
Dim colorValues = System.Enum.GetValues(GetType(Colors))

jmcilhinney said:
By the way, there's actually no such thing as square brackets because all brackets are square. What people often refer to as "round brackets" are actually parentheses, what's referred to as curly brackets are
actually braces and what's referred to as angled brackets are actually chevrons. What's referred to as square brackets are just brackets and only they are brackets. I blame the education system.
According to Bracket - Wikipedia, the free encyclopedia that is a category more than a specific set.
 
Shifting the topic to braces:

VB.NET:
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)


    'Code calling TcpServer
        ' Receive the TcpServer.response. 


         ' Buffer to store the response bytes.
         data = New [Byte](256) {}

What is the purpose of the squiggly ones?
 
Back
Top