brackets around types?

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

i was just looking at the tcplistener example on the MSDN docs and saw this

Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

the data types ar ein brackets, what is the significance of the brackets?
and when i type them into vb.net 2003 the brackets remove themself... why
are they there, what do they do? thanks!
 
Brian Henry said:
i was just looking at the tcplistener example on the MSDN docs and
saw this

Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

the data types ar ein brackets, what is the significance of the
brackets? and when i type them into vb.net 2003 the brackets remove
themself... why are they there, what do they do? thanks!

In your example the brackets are superfluous.

To use reserved keywords as identifiers, the brackets must be used to
distinguish between the identifier and the keyword:

dim [String] As String

public sub [Stop]
end sub

see also:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconKeywordsAsControlNamesInCode.asp
 
Hi Brian,

In your examples:

Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

they do absolutely nothing (which is why they get removed without loss).

In this, though:

Dim [Byte](1024) As Byte
Dim [String] As String = Nothing
[String] = "Poor choice of variable name"

the [] around the names allows you to use a reserved word. The compiler
won't object because you're telling it that you know what you're doing.

Very few people would want to call their variables Byte or String but it's
allowed when [] is used.

Here's a decent example:
Class StopWatch : Inherits Clock
Public Sub Start
'Note the start time.
End Sub

Public Sub [Stop]
'Record the time taken.
End Sub
End Class

'Stop' is a reserved word but it's more than reasonable that a StopWatch
has a Stop method!! So it needs the [] around it in the declaration.

However, in use, unlike the [Byte] and [String] above, you wouldn't need
the [] with Stop because it will be partnered with a StopWatch object.

oStopWatch.Start
'Do something
oStopWatch.Stop
Console.WriteLine (oStopWatch.Elapsed ("ms"))

For the compiler there's no possibilty that the 'Stop' in oStopWatch.Stop
is a reserved word so the [] are not needed.

Regards,
Fergus

--
(Please ignore this - there's a feud going on)
==================================================
Quote of the day
Herfried:
I don't need/want human interaction.
==================================================
 
makes me kinda wonder why MSDN put them in there documentation for
tcplistener...
 
Hi Brian,

I have the 2001 MSDN Help at home and there's an old version of that
code with [Byte] but the new one on the MSDN has more of them. Maybe
it was a translation from some C# by someone who wasn't quite sure? It's
certainly wierd. I mailed them some feedback from the link at the bottom.

Regards,
Fergus
 
* "Brian Henry said:
i was just looking at the tcplistener example on the MSDN docs and saw this

Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

the data types ar ein brackets, what is the significance of the brackets?
and when i type them into vb.net 2003 the brackets remove themself... why
are they there, what do they do?

See:

<http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconkeywordsascontrolnamesincode.asp>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
that is what threw me off... the fact it was around the data type, i knew
about reserved words with brackets because of working with sql server and
similar circumstances... but brackets around a data type just was like what
the hehe..

thanks everyone
 
Back
Top