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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Linq Select From List 3 139
C# XML Get Values 4 53
Connect Gridview column to Textbox in C# 2 55
asp.net, radiobuttonlist, c# 3 69
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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