Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-01
4
Medium Priority
?
1,937 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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

718 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