Parsing Version Numbers

Here are two ways to parse out a string representation of a version number. Start with a version number like “v7.10?. End up with the version number (7) and the revision number (10).

Sub GetVersion()
   
    Dim lVer As Long
    Dim lRev As Long
    Dim dTemp As Double
   
    Const sVer As String = “v7.10”
   
    dTemp = Val(Replace(sVer, “v”, “”))
    lVer = CInt(dTemp)
    lRev = (dTemp – lVer) * 100
   
    Debug.Print lVer, lRev
   
    lVer = Split(Split(sVer, “v”)(1), “.”)(0)
    lRev = Split(Split(sVer, “v”)(1), “.”)(1)
   
    Debug.Print lVer, lRev
   
End Sub

I like the second way. Is it too hard to read? Should I care?

Posted in Uncategorized

5 thoughts on “Parsing Version Numbers

  1. Dick –

    It seems to me versions and revisions aren’t like numbers. That is v7.2 comes before v7.10. Only the second way gets this always right. When tested with v7.2, the first way returns iRev = 20, and when using v 7.8 the first way returns lVer = 8 and lRev = -20, meaning I think that CInt rounds up?

    …mrt

  2. A slightly more efficient way to implement the second method…

    lVer = Mid(Split(sVer, “.”)(0), 2)

    lRev = Split(sVer, “.”)(1)

  3. No regular expressions?
    I’m sure fzz is writing one up this very moment.

  4. “No regular expressions?”
    I’m with K on this one.
    Any utility code that is restricted to the letter “v” isn’t utility.

    However code that recognizes a series of {digit-strings} separated by {period} and bounded by non-{digits} and non{period} would be utility.

    That said, both examples are a good starting point.
    Who hasn’t ultimately developed utility code from a hastily-written specific procedure that was needed yesterday?


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.