+ 1
How do I use strings with user input in C# without encountering a cannot convert error?
I was working on a C# calculator, and as I was testing the input, I encountered cannot convert string to int and int to string.
3 Antworten
+ 2
Random Coder
int a = Convert.ToInt32(Console.ReadLine());
String op = Console.ReadLine();
int b = Convert.ToInt32(Console.ReadLine());
+ 1
if you are a novice programmer, And you've been challenged in conversions، A͢J answer is the best way.
In C#, there are two basic types of type conversion:
Implicit Type Conversions
Explicit Type Conversions
On the opposite side, there is another error. This type of error occurs when user inputs are null or contain non-numerical characters.
The answer is dependent on your code which is developed for WinForms, WPF, web applications, etc.,
Try-catch, regex, and checking all input characters could help you manage user code.
Asp.net and WPF have the most straightforward way to solve the problem
In WinForms, I think you can use Mask for textBox to accept numeric characters.
+ 1
AJ & hossein B, Thanks for the solution and explanation of conversions.