Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
CSS
concatenate the "string" and "int" into int type variable


e.g

int a=10;
string b="abc"
a = a+ b;



is it possible???
how???
Posted
Comments
CHill60 18-Mar-13 7:08am    
"abc" is not an int ... what result are you expecting??
[no name] 18-Mar-13 7:13am    
i want to show like abc(10)
actually this code in VB is working
i just want to convert it in c#
Richard MacCutchan 18-Mar-13 7:25am    
Use a.ToString().

C#
int a=10;
string b="abc"
a = a + b; // NOT POSSIBLE  because integer can not contain character it allows only numbers(degits)
b = b + a.ToString(); // POSSIBLE  because string can contain numbers, chars and special chars;

Happy Coding!
:)
 
Share this answer
 
v2
Hi,
Some basic stuff,

Let say that I give you 10 apples and 5 oranges and ask you how many apples you have, and you reply 15 apples. Does that make any sense to you?

int is short for integers, in English it means numbers i.e. 0,1,2,3,.....
string is an array of char ( short for Characters) 'H' 'e' 'l' 'l' 'o'.

you can convert an int to string, the computer magically get the corresponding character for you. But you cannot convert a string to an int unless that string corresponds to a number.

For example:
C#
int a = 10;

string aToString = a.ToString();  // yes this is possible because string can take any char represented value.

string b = "B";

int bToInt = int.Parse(b);
int bToInt = Convert.ToInt32(b);


Neither of the above code will work, because b is not a representation of number.

However, if the value of b is a representation of a number, then the above codes will convert the string to int.

In your solution 2 you making it even worse by assigning to an object. An object is a type that doesn't know whether the value you assigned is a number or character, its like a basket that you do not know whether it has apples or oranges. If you happen to have oranges in the basket and ask to convert the contents of the basket to apples, it is an impossible task. the same applies to your solution 2, it will throw exception.


I hope you understand this.

Regards
Jegan
 
Share this answer
 
C#
int num = 1000;
string texto = "Texto";
string resultado = string.Concat(num.ToString(), texto);
 
Share this answer
 
Integers cannot be concatenated.
Only numeric operations can be performed on them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900