Solved

How do you convert a string to a value and then back to a string again in C#?

Posted on 2008-09-29
5
231 Views
Last Modified: 2012-05-05
I have a string variable called Zip and I need to add 1 to the zip code but the variable is a string and C# does not allow you to add values to string variables.  Can someone show me the syntax on how to convert it to an integer so I can perform the addition and then convert it back to string again?

Here is my basic code:

        string OriginZip;
        OriginZip = tbOriginZip.Text;
        Session.Add("OriginZip", OriginZip);


string Ozip1;

Ozip1 = OriginZip + 1;
0
Comment
Question by:kwh3856
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22596894
if you know what type it is...

Use Convert.To Int(

or Use TryParse...
0
 
LVL 3

Expert Comment

by:R_Janssen
ID: 22596904
Int.Parse() (or TryParse) to convert anything to a int. TryParse will return a boolean but requires an ref variable declared first. Int.Parse will throw an exception if the value can't be converted.
string MyValue = "10";
int X = Int.Parse(MyValue);
string MyOtherValue = X.ToString();
0
 
LVL 21

Accepted Solution

by:
silemone earned 500 total points
ID: 22596916
string OriginZip;
OriginZip = tbOriginZip.Text;
Session.Add("OriginZip", OriginZip);


String Ozip1;


Ozip1 = Convert.ToString((Convert.ToInt32(OriginZip) + 1));
0
 

Author Closing Comment

by:kwh3856
ID: 31501205
silemone,
I copied Ozip1 = Convert.ToString((Convert.ToInt32(OriginZip) + 1));
straight into my code and it worked perfect.

Thank you both for such a quick response.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22596991
wonderful...good luck proceeding
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question