to convert int to string in text field ( visual studio)
int id = int.parse(textfield1.Text).ToString();
it makes error please do solve this ?
to convert int to string in text field ( visual studio)
int id = int.parse(textfield1.Text).ToString();
it makes error please do solve this ?
You don't need To.String on the end.
Just
int id = int.Parse(textfield1.Text);
the first try that may come to your mind is to do:
var id = textfield1.Text;
Console.WriteLine(id);
or
var id = int.Parse(textfield1.Text);
but that is not safe at all (what if textfield1 is holding something that can not be converted into an integer?)
that is why the best way you take should be.
//here the result of the conversion
var output = -1;
// a variable to verify whether the result was ok or not.
var resultOk = Int32.TryParse(textfield1.Text, out output);
Console.WriteLine(output );
you have a typo parse should be Parse =>int.Parse and you don't need the tostring after that because you are converting it back to string
int id = int.Parse(textfield1.Text);
You are converting it back to string again when calling .ToString()
just use
int id = int.parse(textfield1.Text);
or this to avoid invalid values
int id;
if(int.TryParse(textfield1.Text,out id)){
//Valid value
}
else
{
//Invalid value
}
To convert int to string, you can use one of following methods :
1.
int number = 0;
string value = "5";
number = int.parse(value);
// your code
or 2.
int number = 0;
string value = "5";
number = Convert.ToInt32(value);
// your code
or 3.
int number = 0;
string value = "5";
bool result = Int32.TryParse(value, out number);
if (result == true)
{
// your code
}