Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

The square brackets are to surround names of types or variables in case they are named after a reserved word. For example, if you wanted a variable named Dim, the following wouldn't compile:

Dim Dim As String = "hello"

 

But this would:

Dim [Dim] As String = "hello"

 

Same goes for types. If you wanted, you could declare a new class:

Public Class [Dim]
   Public [For] As String = "hello"
End Class

 

It's there if you needed it, but I think most people would call you crazy for doing this on purpose :)

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
It's there if you needed it' date=' but I think most people would call you crazy for doing this on purpose[/quote']Amen to that.

 

Just for the sake of completeness (and because I'm curious) how would you use such a variable?

Console.WriteLine([Dim].[For] + " and good day!")
' or
Console.WriteLine(Dim.For + " and good day!")

??

Posted

Thanks. Have looked into this further. When you use such a variable you have to use square brackets too.

 

Where you declare a variable as [Object], say, the variable will be taken to refer to the normal type of Object unless you define your own [Object] Class in which case it will be taken to refer to that. So you can use this to use restricted words as variable names or as class names.

 

Apparently one reason for doing this is, if in a later version of VB, a variable or class name that you use becomes a keyword. Then if you've had the foresight to use square brackets, the code will continue to work. Can't think that many people would bother about that.

  • Leaders
Posted
Amen to that.

 

Just for the sake of completeness (and because I'm curious) how would you use such a variable?

Console.WriteLine([Dim].[For] + " and good day!")
' or
Console.WriteLine(Dim.For + " and good day!")

??

Any time a keyword is used as an identifier (not just in the declaration), the brackets are necessary to tell the compiler that the word is an identifier, with one exception: the brackets are optional for an identifier following a dot using dot notation, since the compiler can easily infer that it is being used as an identifier.

' Right
Console.WriteLine([Dim].For.Next.Select.Case.Class & " and good day!")
' Wrong
Console.WriteLine(Dim.For & " and good day!")
' Not necessary
Console.WriteLine([Dim].[For] & "Boogity boo")
' You'll also notice that you can throw brackets where 
' ever you want, whether or not they are necessary.
[Console].WriteLine([MyObject].Member)

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...