message :
: I found this statement in some sample code. It seems to be
: syntactically correct. What do the brackets around "String"
: mean?
:
: Dim sWork As [String] = "Some number of characters"
I don't see the point of using the brackets in this context. I checked
out the complete code you included in the other post and it compiles
just fine with or without the brackets. Just out of curiousity, I
compared both versions of the code in the ildasm utility and the vb
compiler emitted the exact same code. In this instance, the brackets
are meaningless. That said, brackets are quite useful. Indeed, they
are often necessary.
Let's say you have access to an email message class written in C#
which exposes the following Properties:
From
To
Subject
Body
If you were to declare an instance of this class in vb, you'd have a
problem because "To" is a vb keyword. The following would fail:
With New CSharpEmailMessageType
.From = "(e-mail address removed)"
.To = "(e-mail address removed)"
.Subject = "Hello"
.Body = "World"
End With
This would not compile. To get around this, you would need to do the
following:
With New CSharpEmailMessageType
.From = "(e-mail address removed)"
.[To] = "(e-mail address removed)"
.Subject = "Hello"
.Body = "World"
End With
Now the code would compile just fine. Brackets tell the compiler to
treat what would otherwise be a reserved keyword as any other code
element.
Another use I've personally found for the brackets is when I'm
generating throw away code. I don't post to this forum often, but
occasionally I will offer a code example to a question. In those
cases, I may post a complete example that will compile and run. For
example, I might do something like this:
=======================================
Option Strict
Imports Microsoft.VisualBasic
Imports System
Public Module [module]
Public Sub Main
Dim c As New [class]
Console.WriteLine(c.GetMessage)
End Sub
End Module
Public Class [class]
Public Function GetMessage() As String
Return "Hello World" & vbCrLf
End Function
End Class
=======================================
I defined a module and a class but really didn't want to waste time
trying to figure out a clever or relevant name for them, so I just
named them '[module]' and '[class]' and moved on from there.
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *