Hi!
I have written a small method that keeps track of a pivot control containing months from jan-dec. The pivot can scroll both ways and I have to keep track of which way the scrolling occurs, and either reduce or increase the selecteddatetime by one month.
Here is the code so far
I am almost certain this code can be written in a better way, and I am interested in knowing your opinions on how to refactor it?Code:Private Sub CalculateSelectedDate(lastSelected As Integer, selectedPivotIndex As Integer) Dim diff = lastSelected - selectedPivotIndex ' From feb-mar If diff < 0 AndAlso Math.Abs(diff) = 1 Then SelectedDate = SelectedDate.AddMonths(1) ' From mar-feb ElseIf diff > 0 AndAlso Math.Abs(diff) = 1 Then SelectedDate = SelectedDate.AddMonths(-1) ' From dec to jan ElseIf diff < 0 AndAlso Math.Abs(diff) > 1 Then SelectedDate = SelectedDate.AddMonths(-1) ' From jan to dec ElseIf diff > 0 AndAlso Math.Abs(diff) > 1 Then SelectedDate = SelectedDate.AddMonths(1) End If End Sub
What I check is
* which way the navigation is going by comparing last index and current index
* and two special cases for navigating between years
/Henrik




Reply With Quote