Solved

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

Posted on 2004-04-01
4
1,932 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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