Вынесение кода в отдельную функцию - C#
Формулировка задачи:
Есть дублирующийся участок кода. Преподаватель просит вынести его в отдельную функцию. Помогите пожалуйста.
Соответственно в отдельной функции должен быть indexingSimple и indexingWhite
Листинг программы
- private void Form_Load(object sender, EventArgs e)
- {
- dirWhite = FSDirectory.Open (@"C:\White");
- dirSimple = FSDirectory.Open (@"C:\Simple");
- dirStandart = FSDirectory.Open (@"C:\Standart");
- dirSnow = FSDirectory.Open (@"C:\Snow");
- White = new WhitespaceAnalyzer();
- Standart = new StandardAnalyzer(LUCENE_30);
- Simple = new SimpleAnalyzer();
- Snow = new SnowballAnalyzer(LUCENE_30, "Russian");
- Indexing(White, dirWhite,
- public void indexingStandart()
- {
- writer = new IndexWriter(dirStandart, Standart, IndexWriter.MaxFieldLength.UNLIMITED);
- string[] files = System.IO.Directory.GetFiles (folderBrowserDialog.SelectedPath);
- foreach (String file in files)
- {
- string text = System.IO.File.ReadAllText(file);
- Document doc = new Document();
- string name = System.IO.Path.GetFileName(file);
- doc.Add(new Field("name", name, Field.Store.YES, Field.Index.NOT_ANALYZED));
- doc.Add(new Field("text", text, Field.Store.NO, Field.Index.ANALYZED));
- writer.AddDocument(doc);}
- writer.Optimize();
- writer.Dispose();
- }
- public void indexingSimple()
- {
- writer = new IndexWriter(dirSimple, Simple, IndexWriter.MaxFieldLength.UNLIMITED);
- string[] files = System.IO.Directory.GetFiles (folderBrowserDialog.SelectedPath);
- foreach (String file in files)
- {
- string text = System.IO.File.ReadAllText(file);
- Document doc = new Document();
- string name = System.IO.Path.GetFileName(file);
- doc.Add(new Field("name", name, Field.Store.YES, Field.Index.NOT_ANALYZED));
- doc.Add(new Field("text", text, Field.Store.NO, Field.Index.ANALYZED));
- writer.AddDocument(doc);
- }
- writer.Optimize();
- writer.Dispose();
- }
Решение задачи: «Вынесение кода в отдельную функцию»
textual
Листинг программы
- public void myFunction(dynamic dir1, dynamic dir2, MaxFieldLength indexWr)
- {
- writer = new IndexWriter(dir1, dir2, indexWr);
- string[] files = System.IO.Directory.GetFiles (folderBrowserDialog.SelectedPath);
- foreach (String file in files)
- {
- string text = System.IO.File.ReadAllText(file);
- Document doc = new Document();
- string name = System.IO.Path.GetFileName(file);
- doc.Add(new Field("name", name, Field.Store.YES, Field.Index.NOT_ANALYZED));
- doc.Add(new Field("text", text, Field.Store.NO, Field.Index.ANALYZED));
- writer.AddDocument(doc);}
- writer.Optimize();
- writer.Dispose();
- }
- //вызов соответственно
- myFunction(dirStandart, Standart, IndexWriter.MaxFieldLength.UNLIMITED);
- myFunction(dirSimple, Simple, IndexWriter.MaxFieldLength.UNLIMITED);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д