Is there a METHOD that I can use to compare Version Numbers like 1.0.0.1

FartNocker

Regular
Joined
Dec 7, 2003
Messages
97
Location
Western North Carolina
Is there a METHOD that I can use to compare Version Numbers like 1.0.0.1
Visual Basic:
Dim OldVer As Double = Val("1.0.0.1")
Dim NewVer As Double = Val("1.0.0.2")

If NewVer <= OldVer Then
    lblMessage.Text = "Your program is up to date.  Update is NOT necessary. Click Quit to exit."
Else
    lblMessage.Text = "Your program qualifies for the update.  Click Next to begin the update."
End If
Now I know that the above won't work because of the number of decimals. But there some method I can use to compare the two or. will I need to break the version in to two parts to compare it.

Thanks for any help that you may offer

Mark
 
Theoretically, in Binary 1 is lower than 2 so "1.0.0.1" would be Less than "1.0.0.2". But a string compare probably won't work, you can use Split to break the version into 4 blocks of numbers and then compare them:
Visual Basic:
Dim Version() As String, NewVersion() As String
Version = "1.0.0.1".Split(New char() {"."c})
NewVersion = "1.0.0.2".Split(New char() {"."c})

If  Integer.Parse(Version(0)) < Integer.Parse(NewVersion(0)) OrElse _
 Integer.Parse(Version(1)) < Integer.Parse(NewVersion(1)) OrElse _
  Integer.Parse(Version(2)) < Integer.Parse(NewVersion(2)) OrElse _
   Integer.Parse(Version(3)) < Integer.Parse(NewVersion(3)) Then
   'Version is older than NewVersion
Else
   'Same version or NewVersion is older
End If
 
no need for such lengthy code though :)
Visual Basic:
        Dim NewVersion As New Reflection.AssemblyVersionAttribute("1.0.0.2")
        Dim OldVersion As New Reflection.AssemblyVersionAttribute("1.0.0.1")
        If NewVersion.Match(OldVersion) Then
            MessageBox.Show("the versions match!")
        Else
            MessageBox.Show("Oops you have the wrong version!")
        End If
 
I was thinking that but I didn't know if attributes could be created without being attached to things(been a while since I've worked with them).
 
dynamic_sysop I guess that's why you're the Forum Leader.

That is really great that you have all this information in your head.
I have not read any information about comparing versions. Can you tell me where you learned this from because you obviously have better sources than I. I'm sure many will benefit from this code snippet

Again, Thanks a million,

Mark
 
AndreRyan and dynamic_sysop I would like to thank you for your input on this thread regarding version comparison. But after further review I found that nether example would fulfill my needs. Although, the example AndreRyan gave came closer to the objective, there are still times (many) where it will fail. In the two example version numbers above i.e. “1.0.0.2” and “1.0.0.1”, when comparing each element of the arrays there should be matching elements. For instance: the 1st, 2nd, & 3rd numbers should match but still qualify the program for the update and that is where his example fails.

In dynamic_sysop example, I needed to know more about the comparison than if it’s just a match or not. I could not find anyway to use his example other than to find out if the two numbers matched.

What I have come up with below never fails. I know, it looks kinda long for what I need to do. But it’s accurate, dependable and gives the results I’m looking for. If you think you can shorten this code and still get the same results that this code gives, I sure would like to see it.

Again, I can’t thank you guys enough for getting my wheels spinning…

Mark



Visual Basic:
'split version into four parts
        Dim NewVersion() As String = Split("1.0.0.2", ".")
        Dim OldVersion() As String = Split("1.0.0.1", ".")

        'This is used to count the number of times that the test below finds an equal match
        Dim NumberOfEquals As Integer

        'Here I test each element of the arrays ("NewVersion" & "OldVersion") to see if the "OldVersion" is < the "NewVersion". 
        'If "Old" is NOT < NEW then it could be equal. The ELSE part of the IF block checks to see if they were equal and if so, counts
        'the number of Equal matches.

        'NOTE: It would be normal to have some equal matches and and still have a higher version, thus allowing the update.  But is all
        '4 elements were equal then the "NewVersion" IS NOT NEWER than the "OldVersion".
               
        Dim j As Integer
        Dim bNewerVersion As Boolean = True
        For j = 0 To 3
            If Val(OldVersion(j)) < Val(NewVersion(j)) Then
                'Do Nothing
            Else
                If Val(OldVersion(j)) = Val(NewVersion(j)) Then
                    NumberOfEquals += 1
                    If NumberOfEquals = 4 Then 'If there are 4 equals found then there is no need to update.
                        bNewerVersion = False
                        Exit For
                    End If
                Else
                    'here any element found to be less will deny the update
                    bNewerVersion = False
                    Exit For
                End If
            End If
        Next j




        'If boolean variable true then allow the update, otherwise, display a message, hide all buttons but the quit button.
        If bNewerVersion Then
            'Display a Passed message and let the user continue the update
            lblMessage.Text = PassedMessage
        Else
            'display a message, hide all buttons but the quit button.
            lblMessage.Text = FailedMessage
            Button1.Visible = False
            Button2.Visible = False
            Exit Sub
        End If
 
I just made this shorter version of that...

Visual Basic:
                  'split version into four parts
        Dim NewVersion() As String = Split("1.0.0.2", ".")
        Dim OldVersion() As String = Split("1.0.0.1", ".")

        'This is used to count the number of times that the test below finds an equal match
        Dim bNewerVersion As Boolean = False

        For s As Integer = 0 To 3
            If CType(NewVersion(s), Integer) > CType(OldVersion(s), Integer) Then
                bNewerVersion = True
                Exit For
            End If
        Next
        
        'I just display the result here...
        MessageBox.Show(bNewerVersion)

Alex :D
 
Very SHARP AlexCode…….
You are right. Going through the version from left to right, once a greater value is found the test is over. It just never occurred to me like that. Bravo….

Good thread,
Mark
 
.NET includes a full-fledged Version class.

C#:
Version oldVersion = new Version("1.0.0.1");
Version newVersion = new Version("1.0.0.2");
if (newVersion > oldVersion)
	blah;

Visual Basic:
        Dim oldVersion As New Version("1.0.0.1")
        Dim newVersion As New Version("1.0.0.2")
        If Version.op_GreaterThan(newVersion, oldVersion) Then
            'Blah
        End If
 
Back
Top