C# .NET - Adding String to an Integer - Asked By sreya gopal on 13-Jun-12 09:28 AM

How is it possible to add String to an integer (only for displaying).While i tried i'm getting error conversion not possible.


textbox1.text=textbox2.text+textbox3.text.

here textbox2 contains "ABC" and textbox3 contains 1
Peter Bromberg replied to sreya gopal on 13-Jun-12 10:22 AM
First of all, a textbox can only display a string, so you have something else going on before the code you show executes.

Check to see if you are converting the 1 to a string before attempting to assign it's value to textbox3.
Lalit M replied to sreya gopal on 13-Jun-12 12:54 PM
Hi...

you can converting a string to an int is through the Parse or TryParse methods of the System.Int32 struct. read this link

http://msdn.microsoft.com/en-us/library/bb397679.aspx
Chintan Vaghela replied to sreya gopal on 14-Jun-12 12:41 AM

Hi Frndz,

 

Functionality: Concat int with string

 

 

You need to convert int value to string using Convert.ToString()

 

Logic:

 

textbox1.text=Convert.ToString(textbox2.text)+””+Convert.ToString(textbox3.text)


Hope this helpful!

Thanks

 

 

 

TSN ... replied to sreya gopal on 14-Jun-12 12:49 AM

Hi try like below..

 

textbox1.text=textbox2.text.ToString()+textbox3.text.ToString();

Jitendra Faye replied to sreya gopal on 14-Jun-12 01:14 AM
You if want to concatenate string then you can write like this-

textbox1.text= textbox2.text+textbox3.text;

But if you want add TextBoxes value then you need to first convert textBoxes value to Integer type.

Here addition  will be possible if both value can be converted to integer type.
[)ia6l0 iii replied to sreya gopal on 20-Jun-12 10:37 PM
A String can be added to an Integer, if the string's value is an integer represented in the form of string. For e.g. strings like "300", "57" can be added to any Integer. But not strings like "Area51". Hope that makes sense. 

If you just want to display use the ToString() method on the integer and do a concatenation, or simply use String.Format
string displayText = string.Format("string - {0}, int - {1}", stringvariable, intvariable); 

Hope this helps.