Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can not use int j = Int32.Parse(ReadLine()); in visual studio 2019?


What I have tried:

int j = Int32.Parse(ReadLine()); 
Posted
Updated 20-Jan-20 22:35pm
Comments
Richard MacCutchan 21-Jan-20 4:26am    
Then use something else. Seriously, if you want help with a problem then please provide proper detailed explanations. We cannot guess what you are doing, or trying to do.

1 solution

No, because it doesn't know what "ReadLine" method you want to use.
If you are trying to read what the user is typing, then you want the COnsole:
C#
int j = Int32.Parse(Console.ReadLine());

If you are trying to read from a file then you need to specify the Stream that is attached to the file:
C#
int j = Int32.Parse(MyFileStream.ReadLine());
But eitehr way, I'd strongly suggest you use TryParse instead of Parse:
int j;
string input = Console.ReadLine();
if (!Int32.TryParse(input, out j))
   {
   ... report or log a problem here: the input line was not a valid integer ...
   return;
   }
As users are very prone to mistyping and it's "unfriendly" to just crash your app when they do that.
TryParse returns a true / false to say "It worked / it failed" instead of throwing an exception and crashing your app.
 
Share this answer
 
Comments
Rob Philpott 21-Jan-20 9:18am    
StreamReader, rather than the Stream itself, I'd venture in the second case. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900