Solved

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

Posted on 2004-04-01
4
1,933 Views
Last Modified: 2012-08-14
Error: Cannot implicitly convert type 'int' to 'string'
I keep getting this message. How do I take user input from a text box and place it in an integer variable? Also, I know the IsNumeric() test from Visual Basic 6; is there any numeral test like that in C#?
0
Comment
Question by:Miatchguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:eric_duncan
ID: 10738143
You can use the Int32.Parse() method to convert a string to an integer. For example:

string s = "123";
int x = Int32.Parse(s);

will set the variable x to an integer value of 123. For more information about the Int32.Parse() function, including the exceptions it throws, check out the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemint32classparsetopic1.asp

Hope this helps!

Eric
0
 
LVL 2

Accepted Solution

by:
eric_duncan earned 125 total points
ID: 10738156
Sorry - I just realized I didn't answer your exact question, which was how to convert a value in a TextBox to an integer. The code to do that is:

int x = Int32.Parse(textbox.Text);

Eric
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10738921
In addition to eric_duncan's answer: Instead of VB IsNumeric() function catch exception which is thrown by Int32.Parse if string is not numeric:

try
{
    x = Int32.Parse(textbox.Text);
}
catch ( Exception ex )
{
    // string cannot be converted
}

0
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 10740970
In addition:-
The Double.TryParse(...) method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDoubleClassTryParseTopic.asp?frame=true

The big advantage of this is that it does not throw an exception if it can't interpret the string. You should not code expecting to catch exceptions, exceptions take time to propigate and can REALLY effect the performance of your app.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question