4

I am maintaining document version in database. lets say i want to update or increment the version number. i have tried this but it shows me the properties are read only?

string _versionDB = "1.0.0.0"

var version = new Version(_versionDB);
version.MajorRevision = version.Minor + 1; //error it says read only

Please can anyone elaborate me the proper way of increment version number?

3
  • 1
    Because the properties of System.Version are readonly. Why cant you just implement you own functionality to assigning the value Commented Sep 22, 2014 at 7:13
  • Please can you put some sample code here how can i implement my own functionality ? Commented Sep 22, 2014 at 7:16
  • 1
    That would depend on the format you want. Simplest is just to store the value as an int, then increment it. If you really want something like you have shown above, then you would need to split the string, convert the values to numbers, increment one or more, and then join them back to a string Commented Sep 22, 2014 at 7:27

2 Answers 2

8

You could create an extension method to perform the addition for you. Still requires custom code; but if you're doing this in a lot of places having such a helper method is likely useful. Below are a few such functions which may help.

public static class VersionExtension {

    public static Version IncrementRevision(this Version version) {
        return AddVersion(version, 0, 0, 0, 1);
    }
    public static Version IncrementBuild(this Version version) {
        return IncrementBuild(version, true);
    }
    public static Version IncrementBuild(this Version version, bool resetLowerNumbers) {
        return AddVersion(version, 0, 0, 1, resetLowerNumbers ? -version.Revision : 0);
    }
    public static Version IncrementMinor(this Version version) {
        return IncrementMinor(version, true);
    }
    public static Version IncrementMinor(this Version version, bool resetLowerNumbers) {
        return AddVersion(version, 0, 1, resetLowerNumbers ? -version.Build : 0, resetLowerNumbers ? -version.Revision : 0);
    }
    public static Version IncrementMajor(this Version version) {
        return IncrementMajor(version, true);
    }
    public static Version IncrementMajor(this Version version, bool resetLowerNumbers) {
        return AddVersion(version, 1, resetLowerNumbers ? -version.Minor : 0, resetLowerNumbers ? -version.Build : 0, resetLowerNumbers ? -version.Revision : 0);
    }

    public static Version AddVersion(this Version version, string addVersion) {
        return AddVersion(version, new Version(addVersion));
    }
    public static Version AddVersion(this Version version, Version addVersion) {
        return AddVersion(version, addVersion.Major, addVersion.Minor, addVersion.Build, addVersion.Revision);
    }
    public static Version AddVersion(this Version version, int major, int minor, int build, int revision) {
        return SetVersion(version, version.Major + major, version.Minor + minor, version.Build + build, version.Revision + revision);
    }
    public static Version SetVersion(this Version version, int major, int minor, int build, int revision) {
        return new Version(major, minor, build, revision); 
    }

    /*
    //one day we may even be able to do something like this...
    //https://github.com/dotnet/csharplang/issues/192
    public static Version operator +(Version version, int revision) {
        return AddVersion(version, 0, 0, 0, revision);
    }
    public static Version operator ++(Version version) {
        return IncrementVersion(version);
    }   
    */

}

Example Usage

void Main()
{
    Version myVersion = new Version ("1.2.3.4");
    myVersion = myVersion.IncrementRevision();
    Console.WriteLine(myVersion); //1.2.3.5
    myVersion = myVersion.IncrementMinor();
    Console.WriteLine(myVersion); //1.3.0.0
}
Sign up to request clarification or add additional context in comments.

Comments

5

I know this is a bit old, but for anyone looking for how to do this, this may help:

string _versionDB = "1.0.0.0"

var version = new Version(_versionDB);

// Increment Major version
var newMajorVersion = new Version(version.Major+1,version.Minor,version.Build,version.Revision);
// Increment Minor version
var newMinorVersion = new Version(version.Major,version.Minor+1,version.Build,version.Revision);
// Increment Build version
var newBuildVersion = new Version(version.Major,version.Minor,version.Build+1,version.Revision);
// Increment Revision
var newRevisionVersion = new Version(version.Major,version.Minor,version.Build,version.Revision+1);

To return the string to save back to the database (an assumption based on your variable name):

_versionDB = newMajorVersion.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.