what's the meaning of the brackets in Property definition? - vb.net

What is the meaning of the square brackets around the name of a property in the definition ?
Example :
Public Property [Date] As String

To use reserved keywords as identifiers, the brackets must be used to
distinguish between the identifier and the keyword:
dim [String] As String
public sub [Stop]
end sub
On msdn it says:
Any program element — such as a variable, class, or member — can have
the same name as a restricted keyword. For example, you can create a
variable named Loop. However, to refer to your version of it — which
has the same name as the restricted Loop keyword — you must either
qualify it by preceding it with its full namespace, or enclose it in
square brackets ([ ]), as in the following examples:
Reference here

This syntax allows you to use a reserved word as the name of a member or variable. Not recommended though IMHO from a code maintainability point of view (though see comments below for an alternative point of view on this particular point)!
Particularly not recommended if you're going to declare a property called "Date" as a string, but that's a separate issue...

Date is a reserved keyword in VB.NET, but can be used as a property or variable name if enclosed in square brackets:
http://msdn.microsoft.com/en-us/library/ksh7h19t(v=vs.90).aspx

Related

How does MessageBoxIcon Enumeration manage to have Error as part of it

I would like to create an Enumeration, one item of which would be Error. As Error is a keyword the compiler, as expected, points out that that is not on. As I have Option Strict On on by default I thought that possibly this would be one instance where setting it specifically to off would help. However it doesn't.
So put very simply how does the MessageBoxIcon manage to use Error as an item in its enumeration?
Enclose the Error keyword in square brackets
Public Enum SimpleSettings
[Error] = 0
Warn = 1
Correct = 2
End Enum
From the VB.NET Language specifications
Regular identifiers may not match keywords, but escaped identifiers
can. An escaped identifier is an identifier delimited by square
brackets. Escaped identifiers follow the same rules as regular
identifiers except that they may match keywords and may not have type
characters.
Try something like this:
Public Enum Foo
Bar
Faa
[Error]
End Enum

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
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)
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
In VB.Net, <> is used to enclose Attributes.
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
[TemplateContainer(GetType(TemplateItem))]
public ITemplate MessageTemplate { get; set; }
This attribute is used in developing templated controls, which separate data from presentation. In other words, a templated control can retain the same functionality while changing it's appearance.

Common name for variable and constant

In programming (and math) there are variables and constants. Is there a name to describe both of them?
I was thinking value, but that's not it. A value is what variables/constants contain, not what they are.
I would call it a symbol. From google:
sym·bol/ˈsimbəl/Noun
1. A thing that represents or stands for something else,
esp. a material object representing something abstract.
...
From what I know Its called a field
How about:
maths and logic: term
programming: l-value and r-value.
There are a few different terms I use, depending on context. I'll give you a list of the terms I (might) use - sometimes I'll just default to calling everything 'variables'.
Field - a variable or constant that's declared as part of the class definition.
Parameter - one of the inputs specified when defining a method in a class.
Argument - the actual value that you provide for a parameter when calling a method.
Method variable - a variable declared inside a method.
Method constant - a constant declared inside a method.
In OOP, the attribute can be both a variable and a constant.
Identifiers
In computer languages, identifiers are tokens (also called symbols) which name language entities. Some of the kinds of entities an identifier might denote include variables, types, labels, subroutines, and packages.
Symbols are super set of Identifiers
https://en.wikipedia.org/wiki/Identifier#In_computer_languages
How about "data item"?
One definition: https://www.yourdictionary.com/data-item
Example showing it can be used for local variables/constants as well (unlike "field" or "attribute"): https://www.microfocus.com/documentation/visual-cobol/VC222/EclWin/GUID-A3B817EE-1D63-4F67-A62C-61DE681C6719.html

What does [String] mean in VB.NET?

Does anybody know what does the following construct mean:
Dim s1 as [String]
What do the square brackets mean? And why does the following statement with Integer fail while the one above, with String works?
Dim i1 as [Integer]
Thanks in advance.
THe square brackets is used so that the compiler interprets it as a type, even if it would be a keyword. Imagine for example if you had a class named As:
Dim a As [As]
This is usually only used in auto generated code, so that it works with any type that you throw at it.
The reason that you can't use [Integer] is that Integer is not a data type, it's a keyword. You would have to use the corresponding data type, i.e. [Int32].
Square brackets are used to create a variable that has the same name as a keyword in VB.NET. So they are more often used that way:
Dim [Integer] As Integer
Dim [String] As String
In addition to the other answers:
For the case of using variable names with names the same as types: you shouldn't have to use those in your own code. If you do, you are naming your variable names incredibly poorly, and need to work on using better variable names first!

vs String in .NET

I used developerfusion.com to convert a snippet of my C# code to VB .NET and I noticed the String type translated into [String].
I tried Google and Searching SO to no avail so I will ask the community is there a difference between [String] and String? And if so what is/are the difference(s)?
In VB.Net, the [] surrounding a word is used to allow a keyword to be used as a normal identifier. So using [String] means I want to identify something with the word String and not the VB keyword String.
The converter probably did that because it didn't recognise the string type and thought that it was a class defined in your code.
You can put brackets around identifiers to use keywords. You could create your own [String] class that would be different from the built in String class, but that could of course easily get confusing...
Public Class [String]
Public Value As Integer
End Class
Dim s As New [String]
s.Value = 42
Brackets usually indicate a variable name that using a reserved keyword. Did you name string variables String?
Brackets can be used in VB.NET to allow VB.NET keywords to be used as user defined names. For example, you could create a class called Integer, even though there is already an Integer keyword in VB.NET:
Public Class [Integer]
End Class
Hope this clarifies!