3

I am using the Version object in order to run a CompareTo against two version numbers and making X happen if the version is >= versionA.

My issue is that in the below code, the CompareTo is declaring that VersionB is greater than VersionA.

Version versionA = new Version("2.12");
Version versionB = new Version("2.121");

switch (versionB.CompareTo(versionA))
                {
                    case 0: // Equal To
                        _doThis = true;
                        break;
                    case 1: // Greater Than
                        _doThat = true;
                        break;
                    case 2: // Less Than
                        _doNothing = true;
                        break;
                }

This comparison hits case 2. I am using a regex to match a firmware version being passed to the method, as you see here:

^\S+\s(?(\d+.*)+)\s*.*$

I will accept something along the lines of "Version 2.12" and this regex will leave me with just "2.12", which then gets initialized into a Version object.

Anyone have any ideas on why .NET is telling me that Version 2.12 is a newer Version than 2.121?


EDIT:

I have altered my code to be as follows:

Version versionA = new Version("2.12");
Version versionB = new Version("2.121");

if (versionB.CompareTo(versionA) >= 0)
{
     _doThis = true;
}

And it works correctly. Now though, if I compare "2.11" to "2.121", it also returns 1. Shouldn't this comparison return a -1?

Chris Bohatka
  • 363
  • 1
  • 4
  • 14
  • 2
    `the CompareTo is declaring that VersionB is greater than VersionA` -- maybe I'm missing something, but 121 > 12, so isn't this exactly as expected? – Jon B Dec 14 '12 at 17:08
  • VersionB **is** greater than VersionA – Ilia G Dec 14 '12 at 17:09
  • 3
    Yes, the switch statement you have makes no sense for the possible result set. – John Kraft Dec 14 '12 at 17:11
  • I'm wondering how you've got into case 2 when it never returns 2, actually the return range is -1,0,1 – Mahdi Tahsildari Dec 14 '12 at 17:14
  • Get rid of the switch less than is negaitive, 0 is equal, positive is greater than. The greater teh magnitude of the result, teh more different they are. – Tony Hopkinson Dec 14 '12 at 17:21
  • @L.B It returns a signed integer, so it can return 2. You can use it for say compare to n values and pick the closest before or after. – Tony Hopkinson Dec 14 '12 at 17:30
  • When I originally implemented this, I pulled sample code form an outside source and read that it returned 0, 1 and 2. I know now that this is incorrect. Modifying my switch to -1, 0 and 1 appears to resolve my issue. Thank you all. – Chris Bohatka Dec 14 '12 at 17:37
  • It turns out the issue here is due to the versioning numbers I am interacting with. My original case statement was incorrect, but after modifying this, I was still running into issues. My two versions are 2.121 and 2.13. The .NET Version object is simply comparing the minor revision number as thirteen and one hundred twenty-one, meaning 2.13 < 2.121. Thank you all for your help. – Chris Bohatka Dec 14 '12 at 19:21

2 Answers2

5

The Version class provides operator overloads for the comparison operators, why not use those? It really makes the intent of the code clearer.

Meaning you can simply write:

if(versionB >= versionA) {
  _doThis = true;
}

To me, that is much clearer than calling CompareTo and inspecting the sign of the return value.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54
4

Version.CompareTo(Version) returns a signed integer

negative means it's before
zero the same
positive after.

The greater the magnitude of the result the further apart the versions are.

Get rid of the switch, or condition the return of compareto first.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • I was just about to comment on this. My switch statement is incorrect. Sometimes it takes stepping back from the code and getting some outside input to help you realize your mistakes. Thank you! – Chris Bohatka Dec 14 '12 at 17:32
  • I added a new finding to my original post, can you please review this and let me know what you think the cause is? Thank you. – Chris Bohatka Dec 14 '12 at 18:31
  • looks like you've sussed it from your comment – Tony Hopkinson Dec 14 '12 at 21:01