|
-
Jan 11th, 2016, 12:41 AM
#1
Thread Starter
New Member
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.
-
Jan 11th, 2016, 01:28 AM
#2
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.
-
Jan 11th, 2016, 05:37 AM
#3
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)
-
Jan 11th, 2016, 03:49 PM
#4
Re: How to compare 2 versions in VB6
This is how I do it. (sort of the .NET way)
VB Code:
Public Type TVersion Major As Long Minor As Long Build As Long Revision As Long End Type Public Function StringToVer(VerString As String) As TVersion Dim VerSplit() As String VerSplit = Split(VerString, ".") If (UBound(VerSplit()) <> 3) Then Err.Raise 5, "StringToVer()", "Invalid Version String" With StringToVer .Major = VerSplit(0) .Minor = VerSplit(1) .Build = VerSplit(2) .Revision = VerSplit(3) End With End Function Public Function VerHashCode(Ver As TVersion) As Long VerHashCode = VerHashCode Or (Ver.Major And &HF&) * &H10000000 VerHashCode = VerHashCode Or (Ver.Minor And &HFF&) * &H100000 VerHashCode = VerHashCode Or (Ver.Build And &HFF&) * &H1000& VerHashCode = VerHashCode Or (Ver.Revision And &HFFF&) End Function If (VerHashCode(StringToVer("1.2.3.4")) > VerHashCode(StringToVer("1.2.3.4"))) Then 'Whatever... End if
NOTE: This wouldn't work in your case however, because the large Build number.
here's a revised Compare:
VB Code:
Public Function VerCompare(Ver1 As TVersion, Ver2 As TVersion) As Long If Ver1.Major <> Ver2.Major Then If Ver1.Major > Ver2.Major Then VerCompare = 1 Else VerCompare = -1 End If ElseIf Ver1.Minor <> Ver2.Minor Then If Ver1.Minor > Ver2.Minor Then VerCompare = 1 Else VerCompare = -1 End If ElseIf Ver1.Build <> Ver2.Build Then If Ver1.Build > Ver2.Build Then VerCompare = 1 Else VerCompare = -1 End If ElseIf Ver1.Revision <> Ver2.Revision Then If Ver1.Revision > Ver2.Revision Then VerCompare = 1 Else VerCompare = -1 End If End If 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
-
Jan 11th, 2016, 07:58 PM
#5
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
-
Jan 12th, 2016, 02:42 AM
#6
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
-
Jan 12th, 2016, 09:05 AM
#7
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, ",", ".")
-
Jan 13th, 2016, 10:51 AM
#8
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|