Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Incrementing a string field
Message
De
19/12/2010 10:15:54
 
 
À
19/12/2010 09:45:37
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01493214
Message ID:
01493286
Vues:
60
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform