My problem is that the array does not seem to return the correct value, the same value is returned for each data row, but when I display using Response.Write in foreach in Calculation, the correct values are returned. The array values has to be used in a calculation

I've got an array function:

public int[] NumberOfDays(DateTime startDate, DateTime endDate)
    {
        TimeSpan daysCount = endDate - startDate;
        DateTime currDate;
        currDate = DateTime.MinValue + daysCount;
        int monthCount = 0;

        monthCount = (((currDate.Year - 1) * 12) + currDate.Month) - 1;
        int[] result = new int[monthCount];
        int subtractAmount = 0;

        for (int i = 0; i < monthCount; i++)
        {
            DateTime parsingDate = startDate.AddMonths(i);

            if (parsingDate.Month == 12 || parsingDate.Month == 1)
            {
                subtractAmount += 14;
            }

            result[i] = subtractAmount;
        }

        return result;
    }

used in here:

public int Calculation(DateTime start, DateTime end)
    {
int[] result;
            result = NumberOfDays(start, end);
            foreach (int res in result)
            {
                totalHolidays = (result[result.Length - 1]);
                if (res == 0)
                {
                    t1 = t1;
                }
                else
                {
                    t1 = res;
                }
                totalHolidays = (result[result.Length - 1]);
            }
         return t1;
}

Calculator is used here:

foreach (DateTime valuationDate in GetDates(start, end, validDay))
        {
 dt.Rows.Add(valuationDate.ToShortDateString(), 
                Calculation(start, end));
}

Is this web-app?

>My problem is that the array does not seem to return the correct value

Variable t1 is not declared at local scope.

I just didn't put the whole calculation in the post, but t1 is declared,

double t1;
        if (validation > end)
        {
            System.TimeSpan sp_last =
                end.Subtract(start);
            t1 = sp_last.TotalDays;
        }
        else
        {
            System.TimeSpan sp_start =
                validation.Subtract(start);
            t1 = sp_start.TotalDays;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.