3

I am looking at this method in vb.net and I am quite knew to vb.net. I am trying to understand why integer "[to]" and "[step] "is defined in square brackets ? Can some one please explain this to me. Why can these be defined just To/Step. I have attached the code below. Thanks in advance.

   ''' <summary>
    ''' Write to runtime output with loop information
    ''' Expected use when loop counter is incremented
    ''' </summary>
    Public Sub WriteToRuntimeOutput(counter As Integer, [to] As Integer, [step] As Integer)

        Dim message As New StringBuilder
        message.AppendFormat("Loop counter incremented. Loop {0}/{1}", counter, [to])
        If loopStep <> 1 Then
            message.AppendFormat(" Step {0}", [step])
        End If
        message.Append(".")
        return message.ToString()
    End Sub
Nisha
  • 1,783
  • 4
  • 18
  • 34
  • 1
    Possible duplicate of [What do square brackets around an identifier in VB.NET signify?](http://stackoverflow.com/questions/6413343/what-do-square-brackets-around-an-identifier-in-vb-net-signify) – Thomas G Nov 10 '16 at 09:35
  • Hi Thomas , thanks for pointing out. I did look at the question before posting my question. The question above answers for an enum. I understand if a square bracket is used for a enum but my question here is why a square bracket for an integer and why not define "[to]" as counter variable. I have asked this in my question above Thanks anyways for your suggestion. – Nisha Nov 10 '16 at 09:40

3 Answers3

4

Square brackets are used to create a variable that has the same name as a keyword.

For example

Dim [Integer] As Integer

Sameer
  • 705
  • 10
  • 25
1

I voted to close as duplicate but in fact the dup I linked works the other way around and does not really answer your question.

In your case : step and to are reserved keywords in VB. By surrounding them with square brakets you allow their use as variable names.

Thomas G
  • 9,886
  • 7
  • 28
  • 41
1

Square brackets give you the liberty of using keywords as identifiers. For example:

Dim [to] As Integer

makes "to" as an identifier. Although in VB.NET "to" is a keyword but once you put them in bracket [] then you are telling the compiler that it should not be treated as a keyword and rather as an identifier.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331