.Net instance comparison using CompareTo does not work as expected

The Version class in .Net does not implement the CompareTo interface as I expected, it seems to handle the comparison in an alphanumeric way instead of comparing four numbers. Maybe not a bug, but a "feature".

Can anyone suggest why the comparison (as well as the standard <, = and> operators) is not working 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 made their own manual workarounds for this, I would like to know if this is by design, or if it should work and I could do something dumb.

Thank you in advance

Ryan

Update: I was doing something stupid and treating them just like IP addresses. For example; 1.1,3023.5364 is greater than 1.1.5, but 1.1.3023.5364 means <1.1.5000.

0


source to share


1 answer


No, he compares the four parts, treating them as a number. Which is the larger number: 5 or 3251? Most likely it is 3251. Therefore version 1.1.3251. * Is "newer" (ie, greater than) 1.1.5. *.



If you have 1.1.3251 prior to 1.1.5, then you are effectively using one number (the "build" part) as a sequence of numbers. This is mistake.

+4


source







All Articles