13

I have a document management system where documents can have multiple versions. Each version is saved and users can view version history.

What I would like to know is: What datatype should I use for version numbers? Decimal, Float or Double? I'm using .NET and C#.

Version numbers start at 0.1 and each published major version will be rounded to the next whole number. i.e. 0.4 goes to 1.0 and 1.3 goes to 2.0 etc.

When a version numbers hits 0.9 and a minor version is added I would like the number to go to 0.10 not 1.0, when I add to it. This is the biggest issue.

Any suggestions are appreciated.

Thanks.

Jamie
  • 988
  • 1
  • 11
  • 19

5 Answers5

21

System.Version

This already stores the different parts, deals with presenting it as a string (revision and build components are only used in display if they are non-zero, so their irrelevance to your case doesn't matter) and (best of all) is already understood by other .NET developers, and won't lead to confusion (if I saw some use of a version number that wasn't a System.Version I'd spend some time then trying to work out why Version wasn't good enough for the job, in case that proved important and hid a nasty surprise. If it was good enough for the job, I'd be irritated at the developer wasting my time like that).

You can deal with the means you want for incrementing easily with extension methods:

public static Version IncrementMajor(this Version ver)
{
  return new Version(ver.Major + 1, 0);
}
public static Version IncrementMinor(this Version ver)
{
  return new Version(ver.Major, ver.Minor + 1);
}
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Not sure why your solution is so low scored. In my opinion we should always use existing implementations when possible. – Gerald Davis Nov 12 '10 at 14:54
  • 1
    I would love to give t his an upvote, but I have used my quota for the day, so I have to come back and add an upvote tomorrow. Clearly the best answer in here. – Øyvind Bråthen Nov 12 '10 at 15:28
  • At least good that the OP changed the checkmark from my answer to Jon Hanna's to his answer is shown at the top. – Øyvind Bråthen Nov 12 '10 at 15:29
  • What ver.Minor is max value? – Ievgen Feb 02 '13 at 23:23
  • @Evgeny, do you mean what would happen if `ver.Minor` was set at `int.MaxValue` when you called `IncrementMinor`? You'd either get an overflow error or it would change to `int.MinValue` depending on the `checked` or `unchecked` context. This would make compiling the above `checked` (the default) to make sure the exception would happen, a good idea. – Jon Hanna Feb 02 '13 at 23:40
5

How about two integers? One for major and one for minor revisions?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Make your own data type for this

public struct VersionNumber : IComparable<ReleaseNumber>
{
  public int MajorVersion{get;set;}
  public int MinorVersion{get;set;}

  public VersionNumber( int major, int minor)
  {
    MajorVersion = major;
    MinorVersion = minor;
  }

  public override string ToString(){
    return major + '.' + minor;
  }

  public int CompareTo(VersionNumber other) {
    int result;
    result = MajorVersion.CompareTo(other.MajorVersion);
    if (result != 0) { return result; }
    return MinorVersion.CompareTo(other.MinorVersion);
  }
  public static bool operator <(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) < 0;
  }
  public static bool operator <=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) <= 0;
  }
  public static bool operator >(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) > 0;
  }
  public static bool operator >=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) >= 0;
  }
}

You can also add a comparer so you can check two version numbers to see which one is the highest version of two version numbers for example.

EDIT

Added the comparer logic also for good measure :)

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
1

I would suggest two integers: a major and a minor. You can even store this as major * 1000 + minor if you want one variable.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
1

Decimal should be the best of the above given, but as other has noted two ints would be better.

Doubles and floats does not accurately store all decimal values, you don't want your version to suddenly be 1.219999999999999999

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108