469,644 Members | 2,037 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Version comparison for update method

HaLo2FrEeEk
404 256MB
I've written an update method for one of my programs and for the most part it works well, but there is a slight issue. The way I have it check for updates is to simply download an XML file from my server and check the major, minor, and revision numbers in the file against those of the application. So if my application version is 2.2.1 and the XML file says 2.2.2, then the program would know an update was available. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. if (updateMajor > major || updateMinor > minor || updateRevision > revision)
  2. {
  3.     // Update stuff
  4. }
The problem is, it doesn't always work how it's supposed to. For example, if I have an application version 2.2.2 and the update file says 2.1.2, it will prompt me to update since the minor version number in the XML is smaller than te application's version.

My question: what would be a better way to check for this? I keep going back to thinking nested if statements, something like:

Expand|Select|Wrap|Line Numbers
  1. if (updateMajor >= major)
  2. {
  3.     if (updateMinor >= minor)
  4.     {
  5.         if (updateRevision > revision)
  6.         {
  7.             // Update stuff here...
  8.         }
  9.     }
  10. }
But something seems wron with that. Maybe it's just me though, does this look right to you guys, or is there a better way?

And for the record, I'm discarding the *actual* revision number in a standard Assembly Version, and substituting it with the Build number. I do this just because I think 4-digit versions are ugly. Personal preference.
Nov 6 '10 #1
1 2305
GaryTexmo
1,501 Expert 1GB
The nested if statement is the same as changing all the || to && in your original one, and it would mean that 2.2.2 -> 2.2.3 wouldn't update.

I wonder, could you just combine them all into a single number? Lets say you're reserving 3 digits for major and minor, then 4 digits for the revision... I think that's fairly common.

You can just shift them into a double and compare the numbers.

Expand|Select|Wrap|Line Numbers
  1. double verNum = major * Path.Pow(10.0, 3.0) + minor + rev / Math.Pow(10.0, 4.0);
So for your cases of 2.2.2 and 2.2.1, they should come out to 2002.0002 and 2002.0001 respectively. You can easily perform a comparison on those.

Does that work?
Nov 6 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by jiangyh | last post: by
reply views Thread by | last post: by
2 posts views Thread by Aamir Ghanchi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.