Is it treated as an escaped name if the member name is enclosed in square brackets ([]) in the Enum statement of the VBA?Is it better not to use this technique?

Asked 2 years ago, Updated 2 years ago, 268 views

The following code is executable.
 About 10 years ago, when I just started studying VBA, like other languages I had experienced (C#, VB.net),
This is ToString(), which I worked hard to get the Enum member name in a string.
 The question is how to use Enum as defined at this time.

Option Explicit

Enum System
    TypeA = 0
    TypeB
    TypeC
    [_END] =-1
End Enum

Enum Area
    West = 0
    East
    [_END] =-1
End Enum

Enum Time
    [09:00] = 0
    [10:00]
    [11:00]
    [12:00]
    [13:00]
    [_END] =-1
End Enum

Function ToString (Optional ByVal As System= System.[_END], Optional ByVal As Area=Area.[_END], Optional ByVal Tas Time=Time.[_END]) As String
    Dimw As String
    US>"w="
    If Notes=System.[_END] Then
        Select Cases
        Case System.TypeA
            w = "TypeA"
        Case System.TypeB
            w = "TypeB"
        Case System.TypeC
            w = "TypeC"
        End Select
    End If
    If Not A = Area. [_END] Then
        If Not w=""Then w=w&""
        Select Case A
        Case Area.East
            w=w&"East"
        Case Area.West
            w=w&"West"
        End Select
    End If
    If Not T = Time. [_END] Then
        If Not w=""Then w=w&""
        Select Case T
        Case Time. [09:00]
            w=w&"09:00"
        Case Time. [10:00]
            w=w&"10:00"
        Case Time. [11:00]
            w=w&"11:00"
        Case Time. [12:00]
            w=w&"12:00"
        Case Time. [13:00]
            w=w&"13:00"
        End Select
    End If
   
    ToString=w
End Function

Subtest()

   Dim E1 As System
   Dim E2 As Area
   Dim E3 As Time
   
   For E1= System.TypeA To System.TypeC
    For E2 = Area.West To Area.East
        For E3 = Time. [09:00] To Time. [13:00]
             Debug.Print ToString (E1, E2, E3)
        Next E3
    Next E2
   Next E1

End Sub


●Question 1
 Is it possible to avoid compilation errors by using [ ] (square brackets) when defining Enum in this code because of the escape of the reserved word?
 I found a similar reason on the MS site (*1), but I am wondering because I feel that the usage in Time [09:00] is different from the purpose of the description.

*1:
https://docs.microsoft.com/ja-jp/dotnet/visual-basic/language-reference/keywords/

The following keywords are reserved and
name of programming elements (such as variables and procedures) Not available.
However, you can avoid this restriction by enclosing the name in square brackets ([]). For more information, see
See Escaped Names in Declared Element Names

●Question 2 Is it better not to use it often like the reminder (*2) from MS?

*2:
https://docs.microsoft.com/ja-jp/dotnet/visual-basic/programming-guide/language-features/declared-elements/declared-element-names

Escaped name
Typically, element names are reserved by Visual Basic (e.g., Case or Friend)
You must not match the . However, you can define the escaped name.This is
It is surrounded by square brackets ([]). Escaped names are less ambiguous by square brackets.
because square brackets eliminate ambiguity. You can match any Visual Basic keyword.
It also uses square brackets when referring to a name later in the code.
Typically, the escaped name should only be used in the following cases:
A previous version of
where the code did not reserve the keyword to be used as the name. Migrated from Visual Basic.Or
in another language where the specified keyword is not reserved. The described code is being manipulated.
Otherwise, if the name conflicts with the keyword, consider renaming the element.
Integrated Development Environment (IDE) makes this easy to do.
See the Refactoring page for more information.

Note: Enum Statement Documentation
https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/enum-statement

When I created this code, I was a beginner at VBA, so I was impressed by the movement of the code and ended up not deepening the reason (knowledge).(I'm embarrassed.)
 Recently, when I reviewed my code, I wanted to know why square brackets ([]) were valid and if this was OK.
 I would appreciate it if you could let me know if you know.

vba

2022-09-30 21:59

1 Answers

The question is VBA, but the document referenced is VB's.I will only explain my view on VB

Escape names are less ambiguous by square brackets

is the basis.Reserved keywords can also be used as names because of the elimination of ambiguity, which is just an example of how to use them.
Conversely, it does not say that it is a special feature that allows only reserved keywords to be treated as names.

Is it better not to use it often?

According to this page, the name is only alphabetic, numeric, and _.When Japanese is used for variable names, it deviates from the rule... but I think I see some examples where Japanese is used instead of following this rule.
Therefore, I think it is quite unclear how far we will follow the rules.

However, using a special name for Enum for ToString implementation is subtle.You should also consider other means such as arrays.

I checked the documentation on the VBA side, but I couldn't find a description of the escaped name.Just in case, there was a use case in Calling a procedure with the same name, so I think it's a valid feature.

SubMain() 
    MyProject.vbp.MyModule.Main 
End Sub


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.