473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Coverting string to int and...

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
Nov 16 '05 #1
3 4085
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:63**********************************@microsof t.com...
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

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Jarod" <Ja***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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

Nov 16 '05 #3
Sorry, kill that ++, I don't know what I was thinking. Just add +1.

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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jarod" <Ja***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Ayo Ogundahunsi | last post by:
Hi, I have this method - GetConnection in a different namespace, DLL so that i can reuse it. namespace MyCompany.ClassLibrary.Data { /// <summary> /// This Class contains database,...
1
by: Prejith | last post by:
hi my input through the Gui would be 02,03,aa,ab, etc.. i would accept take thes inputs using JoptionPane.showInputDialog("Enter the data bytes seperated by a Comma"); say i would input...
0
by: santoshsri | last post by:
Hello, I have written a Console application in C#, it basically imports data from tab delimited text files and save in SQl Server database. I am converting currency format of Mortgage value into...
8
by: Tony B | last post by:
I have a string in a existing php script which is in the form "dd/mm/yyyy" and I need to convert it into a suitable format for mysql which is "yyyy-mm-dd" Is there a neat way of doing this in php ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.