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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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