Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
how to convert int to string in for loop condition. can any one help...

C#
for (int i = 1; i <= InRegistration.BedNo; i++);
{
      ddlbedNo.Items.Add(Convert.ToString(i));
}

this the condition here am taking i is int but i<=InRegistration.BedNo is string how it is convert...

Edit: Code Formatted
Posted
Updated 19-Sep-11 2:39am
v3
Comments
koolprasad2003 19-Sep-11 8:26am    
int C;
C.toString();
--------------------
what is the problem ?
member60 19-Sep-11 8:31am    
u want convert int to string Within for loop or u want to convert the variable in condition of the for . actually not getting your question !
vasanthkarthi 19-Sep-11 8:35am    
now am update my question see that

C#
for(loop logic)
{

Convert.ToString(intName)

}


try that

Edit: Code Formatted

Put it the wrong way round first time!
 
Share this answer
 
v4
It might help you,

CSharpNumericToString[^]

:)
 
Share this answer
 
Despite the probability that this is a homework question...

C#
StringBuilder builder = new StringBuilder();
foreach(int i in collection)
{
    builder.AppendFormat("{0}", i);
}
 
Share this answer
 
Use ToString() to convert int to string

C#
int a=10;
string b=a.ToString();
 
Share this answer
 
If you are trying to convert an integer to a string manually, rather than using the ToString method, then this is homework! So, no code.

But, it isn't difficult. Although a for loop is not the most ideal structure, it can be done.
1) Create a char array to hold the output.
2) Create your for loop, so that it runs from the right hand side of the array, to the left:
C#
for (int i = myArray.Length - 1; i >= 0; i--)
(I lied about the no code)
3) Inside the loop, take the last digit of you integer (using the modulus operator), convert it to a printable digit and insert it in the array at the current position.
4) Remove the digit from the input, by using the divide operator.
5) When the loop ends, you will have the integer converted into a char array. Convert it to a string, using the appropriate string constructor.
Your string now contains the converted integer, fully padded with '0's.
You may wish to remove leading zeros, but that is up to you...
 
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