Incorrect Value assigned is not used in any execution path

Just installed ReSharper, having taken advantage of your generous offer, and I'm wondering what I ever did without it!

However I have found one niggle - it seems rather inclined to suggest that variables are never used when they are! I've had a search for other reports of this, but can't find any recent issues (last report was over a year ago and linked to a bug report saying it was fixed - all other reports of similar issues were years old).

There's nothing particularly unusually about the code I'm seeing this in, for example:


 

string actDateStr = GetValueFromQueryOutput(outputData, "Policy Activation Date.Date");
DateTime actDate = DateTime.MinValue;
DateTime.TryParse(actDateStr, out actDate);
 
policy = new Policy
   {
     PolicyNumber = policyNumber,
     PolicyActivatedCode = GetValueFromQueryOutput(outputData, "Policy Activated.Code"),
     ActivationTime = actDate,

     }

reports actDate not used, and

 
string pdfErrors = string.Empty;
MemoryStream schedule = ConstructScheduleDocument(fileName, fieldValues, out pdfErrors);
...
if (pdfErrors.Length > 0)
{

    ...
}

reports pdfErrors not used.

Is this is known bug?

Regards,
Geoff Beaumont
Integrious Ltd
2 comments
Comment actions Permalink

Hello Geoff,

By the rules of C# a method should always set the value of 'out' parameters, so DateTime.TryParse(actDateStr, out actDate) always changes the value of actDate and therefore initial assignment is redundant. You can rewrite your code without changing semantics:

DateTime actDate;

DateTime.TryParse(actDateStr, out actDate);




Thanks!

0
Comment actions Permalink

Hi Alex,

You are, of course, quite right - that accounts for all the occurrences of this message I'm seeing (barring a couple which are perfectly valid but due to incomplete code, so expected).

It's funny, I've been coding in C# for years and never picked that up as bad practice. Always something new to learn...

Thanks,
Geoff

0

Please sign in to leave a comment.