Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Brackets () {} [] <>

Can someone please fill in the blanks for me, including a brief description of use and perhaps a code snippet? I'm well aware of the top two in particular, but a little hazy on the last one especially:

  • () - Used for calling a function, object instantiation, passing parameters, etc.
  • {} - Used for defining and adding elements to arrays or sets.
  • [] - Used for forcing an object to be treated as a type rather than keyword.
  • <> - Used for... ?

For Example, I see stuff like this all the time, but still not quite sure what the brackets means...

<TemplateContainer(GetType(TemplateItem))> _
Public Property MessageTemplate As ITemplate
like image 311
Chiramisu Avatar asked Jun 13 '12 18:06

Chiramisu


People also ask

What are square brackets used for in VB?

Square brackets are used to enclose identifiers whose spelling would match that of a reserved word. For example, if a class defined a method called Not (perhaps the class was written in a language without a keyword Not ), one could use such a method within VB by enclosing its name in square brackets (e.g. someVariable = [Not] (5) ).

What are parentheses used for in VB NET?

VB.net uses parentheses for, among other things, arithmetic groupings and function parameters (both of which use parentheses in C#), as well as array subscripts and default-property parameters (both of which use brackets in C#), (indexers), etc.

What does <> mean in VB NET?

In VB.Net, <> is used to enclose Attributes. Show activity on this post. VB.NET uses <> for attributes as well as to indicate "does not equal" ( !=) In your example it is just enclosing attributes. That same code in C# would be

What is VB NET basic syntax?

VB.Net - Basic Syntax 1 A Rectangle Class in VB.Net. For example, let us consider a Rectangle object. It has attributes like length and width. ... 2 Identifiers. An identifier is a name used to identify a class, variable, function, or any other user-defined item. 3 VB.Net Keywords


3 Answers

VB.net uses parentheses for, among other things, arithmetic groupings and function parameters (both of which use parentheses in C#), as well as array subscripts and default-property parameters (both of which use brackets in C#), (indexers), etc. It also uses (Of ... ) to enclose a list of types (which would be enclosed in < ... > in C#, with no "Of" keyword.

Braces are used for array or set initialization expressions, and are also used when defining a generic type with multiple constraints (e.g. (Of Foo As {IEnumerable, IDisposable, Class})). Note that the latter usage is only permitted in constraints; it is alas not possible to e.g. Dim MyThing As {IEnumerable, IDisposable, Class}).

Braces are now also used for the New With {} construct:

Dim p = New Person With {.Name = "John Smith", .Age = 27}
Dim anon = New With {.Name = "Jack Smythe", .Age = 23}

Square brackets are used to enclose identifiers whose spelling would match that of a reserved word. For example, if a class defined a method called Not (perhaps the class was written in a language without a keyword Not), one could use such a method within VB by enclosing its name in square brackets (e.g. someVariable = [Not](5)). In the absence of the square brackets, the above expression would set someVariable to -6 (the result of applying the vb.net Not operator to the value 5).

Angle brackets, as noted elsewhere, are used for attributes. Note that in many cases, attributes are placed on the line above the thing they affect (so as to avoid pushing the affected variable past the right edge of the screen). In older versions of vb, such usage requires the use of a line-continuation mark (trailing underscore).

Angle brackets are also used for XML Literals and XML Axis Properties:

Dim xml = <simpleTag><anotherTag>text</anotherTag></simpleTag>
Console.WriteLine(xml.<anotherTag>.First.Value)
like image 89
supercat Avatar answered Oct 24 '22 16:10

supercat


In this case it's used for the Attribute declaration. It can also be used in XML Literals as follows:

<TestMethod>
Public Sub ThisIsATest()
  If 1 <> 0 Then
    Dim foo = <root>
                <child>this is some XML</child>
              </root>
  End If
End Sub
like image 44
Jim Wooley Avatar answered Oct 24 '22 15:10

Jim Wooley


In VB.Net, <> is used to enclose Attributes.

like image 40
Reed Copsey Avatar answered Oct 24 '22 17:10

Reed Copsey