rbulph Posted November 16, 2005 Posted November 16, 2005 I've noticed that you can enclose object types in square brackets when declaring them. What's that all about? Quote
*Experts* Nerseus Posted November 16, 2005 *Experts* Posted November 16, 2005 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 Quote "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
mskeel Posted November 16, 2005 Posted November 16, 2005 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!")?? Quote
rbulph Posted November 16, 2005 Author Posted November 16, 2005 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. Quote
Leaders snarfblam Posted November 16, 2005 Leaders Posted November 16, 2005 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) Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.