Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Coverting string to int and...

0 views
Skip to first unread message

Jarod

unread,
Mar 11, 2005, 3:23:03 PM3/11/05
to
Hey
I have very simple problem... I have label let's say it's lbl. So I need it
to display a counter:
int counter = Convert.ToInt32(lbl.Text) +1
bl.Text = counter;

In VB.Net I made it like this :
lbl.Text +=1

is there any way, to make it one line in C# ?
Jarod

Tim Wilson UNDERSCORE AT PERIOD

unread,
Mar 11, 2005, 3:31:26 PM3/11/05
to
I would assume that VB.Net lets you get away with that because it performs
all the casting for you. There are a few ways in C#, here's one.

lbl.Text = (Convert.ToInt32(lbl.Text) + 1).ToString();

--
Tim Wilson
.Net Compact Framework MVP

"Jarod" <Ja...@discussions.microsoft.com> wrote in message
news:63AD492C-D188-48B6...@microsoft.com...

Nicholas Paldino [.NET/C# MVP]

unread,
Mar 11, 2005, 3:33:49 PM3/11/05
to
Jarod,

You can make it one line in C#, but it won't be as elegant:

// Increment the text of the label by adding one to the number that is on
the label.
lbl.Text = (++Convert.ToInt32(lbl.Text)).ToString();

The reason that works in VB is because it will perform automatic type
conversion. In reality, VB is converting the text to an integer,
incrementing it by 1, then converting back to a string, and then assigning
it back to the property.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Jarod" <Ja...@discussions.microsoft.com> wrote in message
news:63AD492C-D188-48B6...@microsoft.com...

Nicholas Paldino [.NET/C# MVP]

unread,
Mar 11, 2005, 3:37:14 PM3/11/05
to
Sorry, kill that ++, I don't know what I was thinking. Just add +1.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:%234uphmn...@TK2MSFTNGP10.phx.gbl...

0 new messages