Casting Error


In this article, you will know what is Casting Error?

Learn briefly about Casting Error


Error: Cannot implicitly convert type 'int' to 'string'.

string str1 = "abc";
int i = 0;
str1 = i;

Solution: cast ‘int' into ‘string'.


string str1 = "abc";
int i = 0;
str1 = i.ToString();


Comments

Author: Shailesh 09 Aug 2010 Member Level: Gold   Points : 1

use the following code -

string str1 = "abc";
int i = 0;
str1 = Convert.ToString(i);

if there is null in the i then it gives error then if possible use the convert statement to avaoid error

Author: Roshan R Mhatre15 Nov 2011 Member Level: Silver   Points : 0

Shailesh is right.
Always use
Convert.ToString(txtTextBox.Text);

It not throws error if textbox is empty.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: