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
225 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
Throws error on Decrypting String c# code 3 26
Image(2) 3 26
String manipulation 15 49
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now