Click here to Skip to main content
16,010,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void ShowOldMedicalNoteCommandHandler()
      {
          if (null != this.MedicalElements)
          {
              NavigatedIndex = NavigatedIndex + 1;
              var medicalNote = MedicalElements[NavigatedIndex];
              NavigateMedicalNote(medicalNote);
              IsNewestEnabled = true;
              IsOldestEnabled = MedicalElements.Count - 1 != NavigatedIndex;
          }
      }


What I have tried:

C#
private void ShowOldMedicalNoteCommandHandler()
      {
          if (null != this.MedicalElements)
          {
              NavigatedIndex = NavigatedIndex + 1;
              var medicalNote = MedicalElements[NavigatedIndex];
              NavigateMedicalNote(medicalNote);
              IsNewestEnabled = true;
              IsOldestEnabled = MedicalElements.Count - 1 != NavigatedIndex;
          }
      }
Posted
Updated 20-Dec-16 19:09pm

Obviously NavigatedIndex is greater than the MedicalElements.Count so just check for it:
C#
...
NavigatedIndex = NavigatedIndex + 1;
if(NavigatedIndex < MedicalElements.Count)
{
   var medicalNote = MedicalElements[NavigatedIndex];
   NavigateMedicalNote(medicalNote);
   IsNewestEnabled = true;
   IsOldestEnabled = MedicalElements.Count - 1 != NavigatedIndex;
}
else
{
 // handle this case accordingly 
}
 
Share this answer
 
Apparently, the mistake stems from:
MedicalElements[NavigatedIndex]

when the value of
NavigatedIndex
is greater than the number of items in
MedicalElements
minus one, not to forget indices in a collection like array start from zero. To prevent this, use Count [^].
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900