I have a problem with a simple TryParse
in Console Application.
Take a look:
string DepartureTimeString = Console.ReadLine(); // Enter 2015 01 11 22 45 30
// DepartureTimeString = 2015 01 11 22 45 30
string DepartureTimeStringTrim = DepartureTimeString.Replace(" ", "");
// DepartureTimeStringTrim = 20150111224530
Analyseur = int.TryParse(DepartureTimeStringTrim, out Resultat);
// Analyseur = false
The 'Analyseur' is false..
The string is 20150111224530.. It is just an int..
I had check with stop point the data in VS to be sure.
Where is the problem?
Maximum value you can store in int
or Int32
is int.MaxValue or 2,147,483,647.
The value you are trying to store in a variable of type int
is bigger than this value so the operation fail.
You should use long
or Int64
for this value.
I see that you are trying to store a DateTime
in int
. It is not a good practice unless you have a good reason for doing that. you can use DateTime.TryParseExact to convert the input into a DateTime
. If you are using int
as a container you can not be sure whether the user input is a valid date or not but using DateTime
it is possible.
As Tim mentioned here you can use "yyyy MM dd HH mm ss"
as format string in DateTime.TryParseExact
and parse the input value into a DateTime
.