Results 1 to 9 of 9

Thread: [VB6] modLockEnumCase.bas - Enforce Case of Enums

Threaded View

  1. #1

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012

    Post [VB6] modLockEnumCase.bas - Enforce Case of Enums

    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 occasionally 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:

    Private Enum Constants
    End Enum
    #If False Then
        Dim Const1, Const2, Const3
    #End If
    However, if a project contains a lot of Enums and/or a particular Enum has a lot of members, 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.

    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
        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.SetText sBlock
    End Sub


    The code above is now available as an IDE add-in as well. Just compile the ActiveX DLL project in the attached LockEnumCase.zip archive below to a permanent location (e.g., the VB6 IDE's directory). Then load and unload the add-in as needed via the Add-In Manager dialog box. Usage is as simple as selecting the entire Enum block, right-clicking and then choosing the "Lock Enum Case" context menu item. Holding down either Shift key while clicking the menu item will bring up an InputBox that prompts for the suggested output line length.
    Attached Files Attached Files
    Last edited by Bonnie West; May 28th, 2015 at 03:13 AM. Reason: Attached add-in version
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width