Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Incrementing a string field
Message
From
19/12/2010 10:15:54
 
 
To
19/12/2010 09:45:37
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01493214
Message ID:
01493286
Views:
59
>This works
>
>    private void buttonCreatenewversion_Click(object sender, EventArgs e)
>    {
>      char c = Convert.ToChar(CurrentVersion);
>      c++;
>      string newversion =  c.ToString();
>     
>    } // key down
>
>
>This gives me the existing version. Why is that??
>
>    private void buttonCreatenewversion_Click(object sender, EventArgs e)
>    {
>      char c = Convert.ToChar(CurrentVersion);
>      string newversion =  c++.ToString();
>     
>    } // key down
>


It took me a few minutes to figure this one out, Bill, because at first I thought there was something wrong. However, it's not wrong, and here's why. The ++ operator, after the variable will execute last, before the variable will execute first. So, instead of c++, it will work if you use ++c, like so:
// you *do* need to use the parentheses, otherwise the compiler thinks the ++ is on the string
// and you'll get a compiler error.
(++c).ToString();
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform