Calculate version number for comparison?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I have two version numbers - one for my application and another from a
manifest, database, whatever - that isn't important. Now that I have these
two version numbers (in the form major.minor.build.revision) I want to
compare one to see if it is less than the other.

Is there any calculation I can do to convert a version number to a 'real'
number so that I can compare it easily? I've fiddled around with various
methods but nothing works for everything. I have tried to compare the
individual elements one at a time, but there are so many deviations that it
again doesn't work for everything (i.e. 1.9.9.0 is higher than 1.9.8.920).

There seems like there should be some mathematical way to convert this to
some kind of number so my code could simply say something like "if
current_converted_version < new_converted_version then ...'.

Anyone good mathematicians out there?? :-)

Tom
 
Tom,
If you have two System.Version objects, you can simply use Version.CompareTo
& Version.Equals to compare two versions in VB.NET. If you are using C# then
you can use the overloaded operators to compare Versions.

Dim v1, v2 As System.Version
If v1.CompareTo(v2) < 0 Then
' v1 is less then v2
End If


System.Version v1, v2;
if (v1 < v2)
{
// v1 is less then v2
}

Note in VS.NET 2005 (Whidbey, due out in 2005) the overloaded comparison
operators can be used.

Note there are constructors on System.Version that accepts a string in
major.minor.build.revision format or as 1 to 4 integers.

Be certain to read the help on Version.CompareTo method for some important
tips.

Hope this helps
Jay
 
Back
Top