Situation:
I noticed today, when commenting my code, that a single line comment inside of an Enum is ok, but a multi-line comment, with a line break, throws the error:
Invalid inside Enum
For example, the following works:
Option Explicit
Public Enum testing
'My comment text
Value1 = 1
Value2 = 2
End Enum
Private Sub test()
MsgBox testing.Value1
End Sub
But changing the comment to multi-line in the following, fails:
Public Enum testing
'My comment _
text
Value1 = 1
Value2 = 2
End Enum
More specifically, it seems the presence of the line-continuation character is the issue as I can happily perform:
Public Enum testing
'My comment
'text
Value1 = 1
Value2 = 2
End Enum
I looked up the error Invalid inside Enum at MSDN:
Not all types are valid within an enumeration definition. This error has the following causes and solutions:
You tried to specify a string or some other invalid type as the value of an Enum member. The constant expression used to specify an Enum member must evaluate to type Long or another Enum type.
That didn't really illuminate the issue for me so I looked for explicit cases where you can't use a line-continuation character and found the following in How to: Break and Combine Statements in Code (Visual Basic):
You can't continue a comment by using a line-continuation character. The compiler doesn't examine the characters in a comment for special meaning. For a multiple-line comment, repeat the comment symbol (') on each line.
Now, while more experienced hands might look on smiling knowingly, I was surprised to discover this. I have merrily (a.k.a ignorantly) used the line-continuation for multi-line comments without problems for years e.g.
Private Sub test()
'This is another multi _
line
End Sub
So, it seems, to me, that the compiler must either:
1) Employ some context sensitive logic to the handling of the line-continuation character, e.g. inside a Sub
or Function
, or indeed Module
scope is OK but inside Enum
is not.
2) Be updated in some parts and not in others.
Question:
What is the reason behind this apparent inconsistency and the fact that I seemingly can use a line-continuation character for comments in many cases?
Additional references: