2017-01-05 8 views
0

テキストファイル内の単語を見つけて印刷するプログラムがあります。しかし、これは学校の割り当てであり、異なるクラスやインターフェースを使うなど、ある程度のオブジェクト指向プログラミングを使う必要があります。C#での文字列マニピュレーション

私の問題は、メインクラスで呼び出され、メインメソッドで採用されたときに、2つのパブリッククラスがあり、2つのストリング値を出力するということです。

は、コードが

 Console.Write("please enter a file to search for: "); 
     // Call the constructor that has no parameters. 
     GetFilePath Filepath1 = new GetFilePath(""); 
     Console.WriteLine(Filepath1.FilePath); 
     Filepath1.SetFilename("testfile.txt"); 
     Console.WriteLine(Filepath1.FilePath); 

     // Call the constructor that has one parameter. 
     Console.Write("please enter a word to search for in the file: "); 
     GetSearchWord SearchedWord1 = new GetSearchWord(""); 
     Console.WriteLine(SearchedWord1.WordSearch); 
     SearchedWord1.SetSearchTerm("true"); 
     Console.WriteLine(SearchedWord1.WordSearch); 

を次のようにこれらは、主な機能に実装されているこの

public class GetFilePath 
{ 
    public string FilePath; 

    public GetFilePath(string fn) 
    { 
     /// string path = "testfile.txt"; 
     FilePath = fn; 
    } 
    public void SetFilename(string NewFilePath) 
    { 
     FilePath = NewFilePath; 
    } 
} 

public class GetSearchWord 
{ 

    public string WordSearch; 
    public GetSearchWord(string st) 
    { 
     WordSearch = st; 
    } 


    public void SetSearchTerm(string NewSearchTerm) 
    { 
     WordSearch = NewSearchTerm; 
    } 
} 

のように見えます。しかし、私は、以下の文字列にFilepath1.FilePathSearchedWord1.WordSearchを接続する必要が

string FilePath = ""; 
string WordSearch = ""; 

あなたが見ることができるように、瞬間

私の検索機能では、実際に単語を含む行を検索するキー文字列です!

ファイルパスと文字列がSearchedWord1.WordSearchがあり、以来、私がやって試してみましたが、私はachiveしようとしているものの例として

string WordSearch = SearchedWord1.WordSearch; 

を設定している

using (StreamReader fs = File.OpenText(FilePath)) 
     { 
      int count = 0; //counts the number of times wordResponse is found. 
      int lineNumber = 0; 
      while (!fs.EndOfStream) 
      { 
       string line = fs.ReadLine(); 
       lineNumber++; 
       int position = line.IndexOf(WordSearch); 
       if (position != -1) 
       { 
        count++; 
        Console.WriteLine("Match#{0} line {1}: {2}", count, lineNumber, line); 
       } 
      } 

      if (count == 0) 
      { 
       Console.WriteLine("your word was not found!"); 
      } 
      else 
      { 
       Console.WriteLine("Your word was found " + count + " times!"); 
      } 
      Console.WriteLine("Press enter to quit."); 
      Console.ReadKey(); 
     } 

を以下のように使用されているWordSearched現在 "true"に設定されています。これは、自分のファイルを検索したいキーワードです。

+0

接続するとどういう意味ですか? –

+0

また、コードには別々の文字列があります(必要なものについてわかりやすく説明しているわけではありません)。 – crashmstr

+1

[プロパティ](https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx)で読むことをお勧めします。これは、C#のプライベートフィールドの取得と設定に対処するより良い方法です。 – juharr

答えて

1

私が正しくあなたの質問を理解している場合、次のコードは、(以下であなたのメインコードを更新)あなたの問題を解決する必要があります

Console.Write("please enter a file to search for: "); 
     // Call the constructor that has no parameters. 
     var filePathInput = Console.ReadLine(); 

     GetFilePath Filepath1 = new GetFilePath(filePathInput); 
     Console.WriteLine(Filepath1.FilePath); 
     Filepath1.SetFilename("testfile.txt"); 
     Console.WriteLine(Filepath1.FilePath); 

     // Call the constructor that has one parameter. 
     Console.Write("please enter a word to search for in the file: "); 
     var searchWordInput = Console.ReadLine(); 
     GetSearchWord SearchedWord1 = new GetSearchWord(searchWordInput); 
     Console.WriteLine(SearchedWord1.WordSearch); 
     SearchedWord1.SetSearchTerm("true"); 
     Console.WriteLine(SearchedWord1.WordSearch); 

変更は、このコードは、ユーザからの入力を取得しているということです。..

+0

これはちょっとうまくいきました。これはおそらく私がやりたいことを正確に行うプログラムを手助けするでしょう。しかし、ええ、私は 'string FilePath =" "を変更しました。 string WordSearch = ""; 'to' var FilePath = Filepath1.FilePath; var WordSearch = SearchedWord1.WordSearch; 'これで動作します。ありがとうございました! – Hangfish