Oldalak

2013. február 4., hétfő

Refactoring használata

1. lépés: a kódblokk kijelölése, amit újra hasznosítani szerentnénk, majd a kijelölésen jobb kattintás. A megjelenő menüből a Refactor->Extract Method... menüpontot kell választani.
2. Lépés: a megjelenő párbeszéd ablakban meg kell adni a metódus nevét.

Az alábbi kódból:

for (int i = 0; i iLen; i++)
{
  int iKey;
  char cLetter;
  do
  {
    iKey = r.Next(65, 65 + (2 * iLen));
    cLetter = (char)iKey;
  } while (sSecret.Contains(cLetter.ToString()));
  sSecret += cLetter;
}

ez a metódus lett:

private static string GenerateWord(int iLen)
{
 string sSecret = "";
  for (int i = 0; i < iLen; i++)
  {
  int iKey;
  char cLetter;

  do
  {
  iKey = r.Next(65, 65 + (2 * iLen));
  cLetter = (char)iKey;
  } while (sSecret.Contains(cLetter.ToString()));

  sSecret += cLetter;
  }
  return sSecret;
}
A metódus hívási módja pedig ez:
sSecret = GenerateWord(iLen);

Nincsenek megjegyzések:

Megjegyzés küldése