Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper suggests but does not perform any refactoring to this code block

In the following method, parameters fromDate and toDate's value are never used because they are overwritten in the body before being read.

static void GetDatesFromMonth(string month, ref DateTime fromDate, ref DateTime toDate)
{
  DateTime userDateTime = TimeHelper.getUserGMTNow();
  DateTime calculatedDate = Convert.ToDateTime(month + " 01," + userDateTime.Year);
  toDate = calculatedDate.AddMonths(1).AddSeconds(-1);
  fromDate = toDate.AddMonths(-12).AddSeconds(1);
}

I am using this code at many places in my class file.

When I run Resharper on my code it shows this message and unlike all its other suggessions it is not able to correct this code block

can anybody help me to rewrite this method with good coding practice.

like image 818
Imran Rizvi Avatar asked Sep 03 '25 05:09

Imran Rizvi


1 Answers

Change the two date parameters to out

static void GetDatesFromMonth(string month, out DateTime fromDate, out DateTime toDate) 

See here for a clarification on out vs ref

Simply, you use out when your method need to return two or more values, the out means 'I will set this values before exiting'. On the contrary ref is more complicated. It means 'I need this values/objects inside this method and I will change them before exit'

like image 63
Steve Avatar answered Sep 04 '25 20:09

Steve