Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

783 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