public string GetPrevItem()
 {
    if (IsValidPrevEntry())
    {
        string item = GetPrevString();
        SetProperties();
         return item;
    }
   else
       return null;
 }
public string GetNextItem()
{
    if (IsValidNextEntry())
     {
        string item = GetNextString();
        SetProperties();
        return item;
     }
     else
       return null; 
 }
Declare 2 delegates whose signature matches the methods we want to replace:
  delegate bool IsValid();
  delegate string GetItem();
 private string GetMyItem(IsValid isValid, GetItem getItem)
 {
   if (isValid())
   {
        string item = getItem();
         SetEventProperties();
         return item;
   }
   else
       return null;
  }        
 private string GetNavItem(Func<bool> isValid, Func<string> getItem)
Now, we can call the new method, passing the methods that we called before refactoring, as parameters:
string strPrev =   GetMyItem(IsValidPrevEntry, GetPrevString);
string strNext =   GetMyItem(IsValidNextEntry, GetNextString);
 
No comments:
Post a Comment