4

I have this piece of code :

//EDIT
DateTime finalDate;
Dictionary<string, string> a = new Dictionary<string, string>() { { "a", "2016-04-14T11:56:56.0319859+02:00" } };
string dateStr;
DateTime date;
if (json.TryGetValue("a", out dateStr) && DateTime.TryParse(dateStr, out date))
{
   finalDate = date;
}

But I have the error inside the if "Use of unassigned local variable 'myDate'". If I'm not wrong, if this part is executed myDate has to be assigned by TryParse (technically even if the condition was false it should also be assigned), if I put the TryParse inside the if it works. Is there a way to tell the compiler that it is ok ?

Gatoyu
  • 642
  • 1
  • 6
  • 22
  • 2
    Can you please post an [MVCE](http://stackoverflow.com/help/mcve)? I can't make it fail in the way you describe (using Linqpad). It would fail if we were using `||`, but not `&&`... – RB. Jun 15 '16 at 08:38
  • 2
    I cannot reproduce this compiler error in VS2015, can you show an example that really produces this error? Or in which IDE do you get that error? Is it a compiler error or do you use any other static analysis plugin? – René Vogt Jun 15 '16 at 08:45
  • 7
    I would guess that the important part you haven't mentioned is that `json` is declared as `dynamic`. This is a [known issue](http://stackoverflow.com/questions/16305287/why-does-this-null-tryparse-conditional-result-in-use-of-unassigned-local) in such a case. – Damien_The_Unbeliever Jun 15 '16 at 08:46
  • @Damien_The_Unbeliever wow, tested it, if I declare `json` as `dynamic` the error appears. – René Vogt Jun 15 '16 at 08:48
  • @Damien_The_Unbeliever Wow - that's some Raymond Chen levels of [psychic debugging skill](https://www.google.co.uk/search?q=raymond+chen+psychic+debugging+skills&oq=raymond+chen+psychic+debugging+skills&aqs=chrome..69i57.5479j0j7&sourceid=chrome&ie=UTF-8)!!! This also highlights the importance of a MVCE!! – RB. Jun 15 '16 at 08:48
  • @Gatoyu Your edit doesn't compile. Please test it before posting, so we know that the code will actually reproduce the error (if I change the variable `a` to `json` it won't reproduce your error). – RB. Jun 15 '16 at 08:49
  • @Damien_The_Unbeliever yes json is dynamic but I edited the code wih a regular dictionary and there is still the error – Gatoyu Jun 15 '16 at 08:49
  • 4
    @Gatoyu - I would like to believe you, but the updated code is still using `json` rather than `a` it's still incomplete and, if that's the version you ran, it still didn't *use* the `Dictionary`. – Damien_The_Unbeliever Jun 15 '16 at 08:51
  • 1
    @Damien_The_Unbeliever my bad, so it is because of the dynamic, thank you. – Gatoyu Jun 15 '16 at 09:01

2 Answers2

1

Solved by this comment :

I would guess that the important part you haven't mentioned is that json is declared as dynamic. This is a known issue in such a case.

By Damien_The_Unbeliever

Gatoyu
  • 642
  • 1
  • 6
  • 22
0

It is not guaranteed to be set - if statements are short-curcuited if the first condition is not met.

You need to set myDate to a value

 DateTime myDate = DateTime.MinValue;
Jamiec
  • 133,658
  • 13
  • 134
  • 193