2011-12-30 8 views
5

私はこれを書いて、次のエラーが発生します。変数をお互いに見せる簡単な方法はありますか?クラスとメソッドの間で変数を共有する

警告1変数 'notepad_running'は割り当てられていますが、その値は使用されません。

エラー2 'notepad_running'という名前は、現在のコンテキストに存在しません。

エラー3 'notepad_list'という名前は現在のコンテキストに存在しません。

public class notepad_check_class 
{ 
    public static void notepad_check() 
    { 
     Process [] notepad_list = Process.GetProcessesByName("notepad"); 
     if (notepad_list.Length > 0) 
     { 
      int notepad_running = 1; 
     } 
    } 
} 

public class kill_notepad_class 
{ 
    public static void kill_notepad() 
    { 
     notepad_check_class.notepad_check(); 
     if (notepad_running = 1) 
     { 
      if (MessageBox.Show("Are you sure you want to kill all notepad processes?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
      foreach (Process notepad_process in notepad_list) 
      { 
       notepad_process.Kill(); 
      } 
      return; 
     } 
     else 
     { 
      MessageBox.Show("Cannot find any running process of notepad.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return; 
     } 
    } 
} 
+0

を知っていました。 SE](http://codereview.stackexchange.com)?そこにフィードバックを集めるかもしれないコードには、いくつかの設計上の問題があります。 – Adam

答えて

1

あなたはそれらpublic static作ることができます。これは、コンパイルエラーを修正するためのリファクタリングされた大まかなコードです。

public class notepad_check_class 
{ 
    public static Process[] notepad_list; 
    public static bool notepad_running; 

    public static void notepad_check() 
    { 
     notepad_list = Process.GetProcessesByName("notepad"); 

     notepad_running = notepad_list.Length > 0; 
    } 
} 

public class kill_notepad_class 
{ 
    public static void kill_notepad() 
    { 
     notepad_check_class.notepad_check(); 

     if (notepad_check_class.notepad_running) 
     { 
      if (MessageBox.Show("Are you sure you want to kill all notepad processes?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
       foreach (Process notepad_process in notepad_check_class.notepad_list) 
       { 
        notepad_process.Kill(); 
       } 
      return; 
     } 
     else 
     { 
      MessageBox.Show("Cannot find any running process of notepad.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return; 
     } 
    } 
} 
0
public class notepad_check_class 
    { 
     public int notepad_running; 
     public static void notepad_check(notepad_check_class npc) 
     { 
      Process [] notepad_list = Process.GetProcessesByName("notepad"); 
      if (notepad_list.Length > 0) 
      { 
       npc.notepad_running = 1; 
      } 
     } 
    } 

    public class kill_notepad_class 
    { 
     public notepad_check_class npc; 
     public kill_notepad_class() { 
      npc = new notepad_check_class(); 
     } 
     public static void kill_notepad() 
     { 
      notepad_check_class.notepad_check(notepad_check_class npc); 
      if (npc.notepad_running = 1) 
      { 
       if (MessageBox.Show("Are you sure you want to kill all notepad processes?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
        foreach (Process notepad_process in notepad_list) 
        { 
         notepad_process.Kill(); 
        } 
       return; 
      } 
      else 
      { 
       MessageBox.Show("Cannot find any running process of notepad.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 
     } 
    } 

ダダ!

+1

このコードは、オブジェクトインスタンスが静的コンテキストからアクセスされたため、 'if(npc.notepad_running = 1)'行にオブジェクトインスタンスが必要であると訴えるでしょう。 'notepad_running = 1;でも同じエラーです。 –

+0

完全に正しいです。これを修正しました。 ** real **ソリューションの場合は –

5

あなたはnotepad_check_classにパブリック静的プロパティを置くことによってそれを行うことができます:

public static Process[] NotepadList { set; get; } 
public static int NotepadRunning { set; get; } 

私はただ一つのクラスを示唆しているしかし:あなたはあなたのコードは[コードレビューに見直さ取得することができ

public static class NotepadManager { 

    private static Process[] NotepadList { set; get; } 
    private static int NotepadRunning { set; get; } 

    public static void Check() { ... } 
    public static void Kill() { ... } 

} 
+0

+1です。側の小さなメモ: 'Check()'はおそらく 'private'で、' Kill() 'は何か他のことをする前に' Check() 'を呼び出すことができます。 – Adam

関連する問題