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
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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