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

FartNocker

Well-known member
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
Code:
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 wont 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 wont work, you can use Split to break the version into 4 blocks of numbers and then compare them:
Code:
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 :)
Code:
        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 didnt know if attributes could be created without being attached to things(been a while since Ive worked with them).
 
dynamic_sysop I guess thats why youre 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. Im 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.
 
I just made this shorter version of that...

[VB]
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)
[/VB]

Alex :D
 
.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;

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