Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now