What Does This Mean?

G

Guest

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"
 
M

Michael C

Herman Jones said:
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"

They are used indicate the item in the brackets is not a keyword. For
example if you wanted to name a variable Dim you might be able to do this:

Dim [Dim] as Int32
[Dim] = 5

I'm not sure if that works but that the sort of thing it's used for. It
could be used for function names, class names etc. In the case of String
above it does nothing but doesn't cause any problems either. You can remove
them.

Michael
 
G

Guest

This example seems to be a little different. In your example you're using
[Dim] as a variable name. It comes before the "As" keyword.

The sample code I found has [String] after the "As" keyword. I don't thing
that "String" is being used as the variable name because the program later
refers to the "sWork" variable.

Is it possible that the brackets have some other purpose when they bracket a
datatype?



Michael C said:
Herman Jones said:
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"

They are used indicate the item in the brackets is not a keyword. For
example if you wanted to name a variable Dim you might be able to do this:

Dim [Dim] as Int32
[Dim] = 5

I'm not sure if that works but that the sort of thing it's used for. It
could be used for function names, class names etc. In the case of String
above it does nothing but doesn't cause any problems either. You can remove
them.

Michael
 
M

Michael C

Herman Jones said:
This example seems to be a little different. In your example you're using
[Dim] as a variable name. It comes before the "As" keyword.

The sample code I found has [String] after the "As" keyword. I don't
thing
that "String" is being used as the variable name because the program later
refers to the "sWork" variable.

Is it possible that the brackets have some other purpose when they bracket
a
datatype?

The dim was just 1 example, it can be used in many places. If a program had
a datatype called "As" then you'd need to do Dim x as [As]

Michael
 
C

Cor Ligthert [MVP]

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"

That the programmer of this program is probably an idiot.
You replace with this the general meaning of the keyword.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As New [String](1)
Dim b As String = "What the hell is this"
End Sub
End Class
Public Class [String]
Public Sub New(ByVal b As Integer)
b = b
End Sub
Public b As Integer
End Class

Cor
 
B

Brian Gideon

Herman,

No, the bracket does not have alternate meaning when used with data
types. Brackets allow you to create escaped or verbatim identifiers.
This is useful when you need to refer to an identifier that is also a
keyword. The most common scenario is the interaction between different
programming languages. For example, a C# programmer may think Alias
would be a good choice for a property name not knowing that it is a
keyword in VB. A VB programmer would need to use brackets when
referring to that property.

Brian
 
B

Brian Gideon

Herman,

No, the bracket does not have alternate meaning when used with data
types. Brackets allow you to create escaped or verbatim identifiers.
This is useful when you need to refer to an identifier that is also a
keyword. The most common scenario is the interaction between different
programming languages. For example, a C# programmer may think Alias
would be a good choice for a property name not knowing that it is a
keyword in VB. A VB programmer would need to use brackets when
referring to that property.

Brian
 
A

_AnonCoward

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
 
C

Cor Ligthert [MVP]

_Anon,

Nobody discuss that they can be handy. I do not believe that there is a
serious person who would give String another name even not somebody from who
is selling underwear.

Cor

_AnonCoward said:
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
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top