Results 1 to 8 of 8

Thread: How to compare 2 versions in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2013
    Posts
    14

    How to compare 2 versions in VB6

    I have 2 versions, lets say 1.0.2016.1000 and 1.0.2015.2000, out of which the first one is higher version.

    How to do these comparision in vb6?

    If both are same, it should return true without any looping. Else, it should do a minimum looping mechanism and return true if first version is greater and false if it is lesser.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to compare 2 versions in VB6

    In version numbering the last portion is typically the build number and will always be higher if the build is newer even if there were no changes at all to the code.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: How to compare 2 versions in VB6

    "Typically"? You sure have a lot of faith in programmers to follow conventions... definitely don't rely on that in production code even if it is 'typical' since it's ignored too much to trust.

    If you need to be sure you need to compare each one. I'd be hesistant to even trust there always being 4 numbers (e.g. VB by default only has 3), but if it's only for the same file from the same company it should be safe, so:
    (pseudocode to show concept)

    Dim sVersion1() As String, sVersion2() As String
    sVersion = Split(fullversion, ".")
    if clng(sversion2(0)) > clng(sVersion1(0)) then return true and exit
    elseif clng(sVersion2(0)) < clng(sVersion1(0)) then return false and exit

    (so checks only continue if they're equal. repeat for 1, 2 and 3, or put into a for i = 0 to ubound if you want to guard against 3-entry versions. without anything else it would also return false if versions were equal)

  4. #4
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,229

    Re: How to compare 2 versions in VB6

    This is how I do it. (sort of the .NET way)

    VB Code:
    1. Public Type TVersion
    2.     Major           As Long
    3.     Minor           As Long
    4.     Build           As Long
    5.     Revision        As Long
    6. End Type
    7.  
    8. Public Function StringToVer(VerString As String) As TVersion
    9.     Dim VerSplit() As String
    10.     VerSplit = Split(VerString, ".")
    11.     If (UBound(VerSplit()) <> 3) Then Err.Raise 5, "StringToVer()", "Invalid Version String"
    12.     With StringToVer
    13.         .Major = VerSplit(0)
    14.         .Minor = VerSplit(1)
    15.         .Build = VerSplit(2)
    16.         .Revision = VerSplit(3)
    17.     End With
    18. End Function
    19.  
    20. Public Function VerHashCode(Ver As TVersion) As Long
    21.     VerHashCode = VerHashCode Or (Ver.Major And &HF&) * &H10000000
    22.     VerHashCode = VerHashCode Or (Ver.Minor And &HFF&) * &H100000
    23.     VerHashCode = VerHashCode Or (Ver.Build And &HFF&) * &H1000&
    24.     VerHashCode = VerHashCode Or (Ver.Revision And &HFFF&)
    25. End Function
    26.  
    27. If (VerHashCode(StringToVer("1.2.3.4")) > VerHashCode(StringToVer("1.2.3.4"))) Then
    28.    'Whatever...
    29. End if

    NOTE: This wouldn't work in your case however, because the large Build number.


    here's a revised Compare:
    VB Code:
    1. Public Function VerCompare(Ver1 As TVersion, Ver2 As TVersion) As Long
    2.     If Ver1.Major <> Ver2.Major Then
    3.         If Ver1.Major > Ver2.Major Then
    4.             VerCompare = 1
    5.         Else
    6.             VerCompare = -1
    7.         End If
    8.     ElseIf Ver1.Minor <> Ver2.Minor Then
    9.         If Ver1.Minor > Ver2.Minor Then
    10.             VerCompare = 1
    11.         Else
    12.             VerCompare = -1
    13.         End If
    14.     ElseIf Ver1.Build <> Ver2.Build Then
    15.         If Ver1.Build > Ver2.Build Then
    16.             VerCompare = 1
    17.         Else
    18.             VerCompare = -1
    19.         End If
    20.     ElseIf Ver1.Revision <> Ver2.Revision Then
    21.         If Ver1.Revision > Ver2.Revision Then
    22.             VerCompare = 1
    23.         Else
    24.             VerCompare = -1
    25.         End If
    26.     End If
    27. End Function

    VerCompare(StringToVer("1.2.3.4"),StringToVer("0.2.3.4"))
    Last edited by DEXWERX; Jan 11th, 2016 at 04:16 PM. Reason: added VerCompare routine

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to compare 2 versions in VB6

    Another...

    Code:
    Private Sub Command1_Click()
    
        Print VersionCompare("1.0.2016.1000", "1.0.2015.2000")
    
    End Sub
    
    Function VersionCompare(v1 As String, v2 As String) As Boolean
        
        Const pad& = 4
        Dim array1() As String, array2() As String, i as long
        
        array1 = Split(v1, ".")
        array2 = Split(v2, ".")
        If UBound(array1()) <> UBound(array2()) Then MsgBox ("Not comparable")
        For i = 0 To UBound(array1())
            array1(i) = Right$(String$(pad, "0") & array1(i), pad)
            array2(i) = Right$(String$(pad, "0") & array2(i), pad)
        Next
        VersionCompare = Join(array1(), ".") > Join(array2(), ".")
        
    End Function

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: How to compare 2 versions in VB6

    Nice seeing everyone uses his own preferred approach:

    Code:
    Public Function CompareVersions(sVersion1 As String, sVersion2 As String) As Boolean
      Dim aV1() As String, aV2 As String
      Dim i As Long
    	
      aV1 = Split(sVersion1, ".")
      aV2 = Split(sVersion2, ".") 
    
      If UBound(aV1()) <> UBound(aV2()) Then 
        MsgBox ("Not comparable")
        Exit Function
      End If
    
      For i = 0 To UBound(aV1)
        If CLng(aV1(i)) > CLng(aV2(i)) Then
          CompareVersion = True
          Exit For
        End If
      Next i
    End Function

  7. #7
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: How to compare 2 versions in VB6

    FYI - if your example has code like this:

    Code:
    versionArray = Split(v1, ".")
    You need to be careful with how your original version string was retrieved. If it used any functions where locale settings come into play, the separator may be a comma instead of a period. As as "quick and dirty" failsafe, you can preface your check with something like:

    Code:
    If InStr(1, versionString, ",", vbBinaryCompare) Then versionString = Replace$(versionString, ",", ".")
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  8. #8
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: How to compare 2 versions in VB6

    reguardless of how the numbers appear...

    if you add a number of the separators to the fetched value such that a.b.c.d becomes a.b.c.d.... you will prevent the split() versions that rely on x number of separators from failing

    if you fetch the whole thing and replace the separators with nulls you will end up with a large number or string that can be compared directly you will not know the magnitude of the revision , but you will recognise the priority.

    here to help ( simplify )

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