Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a variable PluginVersion of type string with a value of 1.0.1.7
i.e. PluginVersion=1.0.1.7
I wanted to increment it in a way like this:
1.0.1.8 , 1.0.1.9,1.0.2.0..... 1.0.3.0 ..... 1.1.0.0. ... and so


Code Extract :
C#
if (checkExists(pluginRegistrationEntity))
{
    pluginRegistrationEntity.PluginVersion += 1;
    string cmdText = "Update PluginRegistration set PluginVersion='" + pluginRegistrationEntity.PluginVersion + "'where PluginName='" + pluginRegistrationEntity.PluginName + "' && Device Type='" + pluginRegistrationEntity.DeviceType + "' && DeviceManufacturer='" + pluginRegistrationEntity.DeviceManufacturer + "' && DeviceModel='" + pluginRegistrationEntity.DeviceModel + "'";
    SqlHelper.ExecuteReader(Dsn, CommandType.Text, cmdText);
    return 1;
}


How could i achieve this?
Thanks :)
Posted
Updated 17-Jul-14 3:47am
v3
Comments
Pikoh 17-Jul-14 9:36am    
Increment when? How? Are you using clickonce?
Anshumaan Chaturvedi 17-Jul-14 9:39am    
This data is being read from an xml file and before storing it in a particular table of a DB
it needs to be increment in a conditional code section.
Dilan Shaminda 17-Jul-14 9:40am    
Do you want to increment the version of the project on building your application or you just want to increment PluginVersion variable?
Anshumaan Chaturvedi 17-Jul-14 9:43am    
No, only the PluginVersion variable. that's it.
if (checkExists(pluginRegistrationEntity))
{
pluginRegistrationEntity.PluginVersion += 1; /// Errorneous logic
string cmdText = "Update PluginRegistration set PluginVersion='" + pluginRegistrationEntity.PluginVersion + "'where PluginName='" + pluginRegistrationEntity.PluginName + "' && Device Type='" + pluginRegistrationEntity.DeviceType + "' && DeviceManufacturer='" + pluginRegistrationEntity.DeviceManufacturer + "' && DeviceModel='" + pluginRegistrationEntity.DeviceModel + "'";
SqlHelper.ExecuteReader(Dsn, CommandType.Text, cmdText);
return 1;
Pikoh 17-Jul-14 9:45am    
First,instead of pasting code here,click improve question and paste it there so the code shows formatted. Second,your logic seems ok,what error give it to you?

Ok,so your problem is "rounding" your variable to just one digit. There should be thousands of approaches,let me show you just the first it comes to my mind:

C#
string[] version = pluginRegistrationEntity.PluginVersion.Split('.');
version[3] = (int.Parse(version[3]) + 1).ToString();
for (int i = 3; i > 0; i--)
{
   if (version[i].Length > 1)
   {
        version[i] = "0";
        version[i-1] = (int.Parse(version[i-1]) + 1).ToString(); ;
    }
}
pluginRegistrationEntity.PluginVersion = version[0] + "." + version[1] + "." + version[2] + "." + version[3];


I'm sure there are better ways to do it,but i think this works.
 
Share this answer
 
v2
Comments
Anshumaan Chaturvedi 17-Jul-14 10:09am    
Bravo Pikoh... this initiated my thought process of generating the series.
Pikoh 17-Jul-14 10:12am    
Glad to help you :)
What about something like this?

C#
private string IncrementVersion(string v)
{
    int[] n = Array.ConvertAll(v.Split(new char[] { '.' }), int.Parse);

    if (++n[3] > 9)
    {
        n[3] = 0;
        n[2]++;
    }
    if (n[2] > 9)
    {
        n[2] = 0;
        n[1]++;
    }
    if (n[1] > 9)
    {
        n[1] = 0;
        n[0]++;
    }

    return string.Join(".", Array.ConvertAll(n, s => s.ToString()));
}
 
Share this answer
 
Comments
Anshumaan Chaturvedi 17-Jul-14 10:43am    
thanks Praveen. Awesome.
Just another maybe simpler way,just to demonstrate there are literally thousands of ways to accomplish this.

C#
string p = pluginRegistrationEntity.PluginVersion.Replace(".", "");
int pp = int.Parse(p);
pp += 1;
p = pp.ToString().PadLeft(4, '0');
pluginRegistrationEntity.PluginVersion = p[0] + "." + p[1] + "." + p[2] + "." + p[3];
 
Share this answer
 
Comments
Anshumaan Chaturvedi 17-Jul-14 10:37am    
This one is far more easier and optimized. you guys rock.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900