1

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:

1) How to continue the code on the next line in VBA

Community
  • 1
  • 1
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 1
    IMO this inconsistency could have to do with the prevalent effect of underscores to hide Enum member names from object browser and intellisense (after encasing such member names in brackets [], as leading underscores aren't allowed here) at the expense of full comment support. So possibly any underscore would be identified as an invalid naming attempt regardless of the place it occurs. - Cf. Chip Pearson's http://www.cpearson.com/excel/Enums.aspx and an interesting technique at https://www.linkedin.com/pulse/interesting-technique-error-handling-enumeration-vba-chip-pearson – T.M. Mar 05 '18 at 20:57
  • @T.M. Thanks. I will have a look. I am not that advanced so I can't say I fully understand your comment but I will try to. This bit "prevalent effect of underscores to hide Enum member " means? – QHarr Mar 05 '18 at 20:59
  • My comment is based on supposition, as I don't know the actual programming logic of the related basic libraries. Maybe this is a theme worth to be treated by another of our SO gurus (possibly @Mat's Mug with insight of VBA DLLs and libraries). - BTW appreciated your question, as it allows me to learn: I wouldn't have found the second link reference showing a very interesting technique of error handling via `Enum` validation without your question (see https://www.linkedin.com/pulse/interesting-technique-error-handling-enumeration-vba-chip-pearson). – T.M. Mar 05 '18 at 21:36
  • If you name a `Enum` member e.g. `[_First]` the underscore character plays an important role - or even *prevailent* role over pure line breaking - by "preventing those names from appearing in Intellisense (but you need the square brackets [..] to keep the compiler from complaining about the leading underscore character), so you won't see them when you're coding and use the enumeration" – T.M. Mar 05 '18 at 21:45
  • 1
    Pretty sure @Mat's Mug would have come across it. Thanks for the explanation :-) – QHarr Mar 05 '18 at 21:46

0 Answers0