Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1470

[VB6] modLockEnumCase.bas - Enforce Case of Enums

$
0
0
The VB6 IDE has an annoying quirk when it comes to the case of Enum members. Unlike with other identifiers, the IDE doesn't enforce the case of an Enum member as it was declared in the Enum block. That usually causes an Enum member that was manually written to lose its original case, unless a coder typed it carefully enough. The prevalent workaround for this bug is to redeclare the identifiers inside an #If...Then...#End If directive

Code:

Private Enum Constants
    Const1
    Const2
    Const3
End Enum
#If False Then
    Dim
Const1, Const2, Const3
#End If

However, if a project contains a lot of Enums, redeclaring the members in each of them can get quite tedious fast. Nobody seems to have submitted yet a routine to automate this process here in the VB6 CodeBank, so I'm sharing this code snippet I've had for some time now.

Code:

Attribute VB_Name = "modLockEnumCase"
Option Explicit

'modLockEnumCase.bas usage:
'1. Add to project.
'2. Select entire Enum block.
'3. Copy to Clipboard.
'4. Run LockEnumCase() from the Immediate Window. Optionally *suggest* length of each line.
'5. Paste after the Enum block.
'6. Remove from project when no longer needed.


Public Sub LockEnumCase(Optional ByVal LineLen As Integer = 80) 'Adjust length of output lines as desired
Attribute LockEnumCase.VB_Description = "Enforces the case of Enumerations via Conditional Compiler Directives."
    Dim sBlock As String, sLine As String, sText As String, oMatch As Object 'Match

  'See if there's anything to process; quit if no text was copied

    If Clipboard.GetFormat(vbCFText) Then sText = Clipboard.GetText Else Exit Sub
  'Prepend the conditional compiler directive that is set to False
    sBlock = "#If False Then" & vbNewLine
  'Dimension variables that reuses the Enum members' names
    sLine = "Dim "

    With CreateObject("VBScript.RegExp") 'New RegExp
        .Global = True
        .MultiLine = True

      'Strip all comments
      .Pattern = " +'.*$"
        sText = .Replace(sText, vbNullString)

      'Exclude Enum statements
      .Pattern = "(\b(Private|Public)? Enum [A-Za-z]\w*\b)|(\bEnd Enum\b)"
        sText = .Replace(sText, vbNullString)

      'Split multiple expressions in a single line into their own lines
        If InStrB(sText, ":") Then sText = Replace(sText, ":", vbNewLine)

      'This should match most Enum member names, including those enclosed with []
      .Pattern = "^ *([A-Za-z]\w*|\[.+\]) *(?:=|$)"

        For Each oMatch In .Execute(sText)
            sLine = sLine & (oMatch.SubMatches(0&) & ", ")

          'Check if the string being built is exceeding
          'the *suggested* limit of each output line

            If Len(sLine) >= LineLen Then
              'If so, commit this line to the output string
                sBlock = sBlock & (sLine & "_")
              'Begin anew at the next line
                sLine = vbNewLine
            End If
        Next
    End With


  'Finish the conditional compiler directive block, removing empty lines as needed
    sBlock = sBlock & (IIf(sLine <> vbNewLine, sLine, vbNullString) _
                    & vbNewLine & "#End If" & vbNewLine)
  'Overwrite the last comma with a space
    Mid$(sBlock, InStrRev(sBlock, ",")) = " "
  'Try to erase the last underscore on the last line, if present
    On Error Resume Next
    Mid$(
sBlock, InStrRev(sBlock, "_" & vbNewLine & "#")) = " "
    On Error GoTo 0

  'Copy back to the Clipboard
    Clipboard.Clear
    Clipboard.SetText sBlock
End Sub

Attached Files

Viewing all articles
Browse latest Browse all 1470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>