The Version class in .Net does not implement the CompareTo interface as I would expect, it seems to handle the compare alphanumerically instead of comparing the four numbers. Maybe not a bug, but a ‘feature’.
Can anyone shine a light on why the compare (and also the standard <, = and > operators) do not work as I would expect below?
Dim MainVersion As New Version('1.1.3251.4029') Dim Ver_Low As New Version('1.1') Dim Ver_Same As New Version('1.1.3251.4029') Dim Ver_High As New Version('1.1.5.0') ' CompareTo here yields 1 which is expected as MainVersion is greater than Ver_Low. MessageBox.Show(String.Format('{0}.CompareTo({1}) = {2}', MainVersion.ToString(), Ver_Low.ToString(), MainVersion.CompareTo(Ver_Low).ToString())) ' CompareTo here yields 0 which is expected as MainVersion and Ver_Same are the same. MessageBox.Show(String.Format('{0}.CompareTo({1}) = {2}', MainVersion.ToString(), Ver_Same.ToString(), MainVersion.CompareTo(Ver_Same).ToString())) ' **** Issue here **** CompareTo here yields 1 which is NOT expected as MainVersion is less than Ver_High. MessageBox.Show(String.Format('{0}.CompareTo({1}) = {2}', MainVersion.ToString(), Ver_High.ToString(), MainVersion.CompareTo(Ver_High).ToString()))
I know people have done their own manual workarounds for this, I’d like to know if this is by design or it should work and I might be doing something dumb.
Thanks in advance
Ryan
Update: I was doing something subtly dumb and treating them in the same was as IP addresses. For example; 1.1.3023.5364 is greater than 1.1.5 but 1.1.3023.5364 is < than 1.1.5000.
No, it’s comparing the four parts, treating each as a number. Which is the bigger number: 5 or 3251? Surely it’s 3251. Therefore version 1.1.3251.* is ‘newer’ (i.e. greater) than 1.1.5.*.
If you’ve got 1.1.3251 coming before 1.1.5, then you’re effectively using a single number (the ‘build’ part) as a sequence of digits. That’s a mistake.