2011-12-30 36 views
5

Tôi đã viết điều này và tôi nhận được các lỗi sau. Có cách nào đơn giản để làm cho các biến nhìn thấy nhau không?Chia sẻ các biến giữa các lớp và phương thức

Cảnh báo 1 Biến 'notepad_running' được chỉ định nhưng giá trị của nó không bao giờ được sử dụng.

Lỗi 2 Tên 'notepad_running' không tồn tại trong ngữ cảnh hiện tại.

Lỗi 3 Tên 'notepad_list' không tồn tại trong ngữ cảnh hiện tại.

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

bạn có biết bạn có thể nhận mã của bạn xem xét tại [codereview. SE] (http://codereview.stackexchange.com)? Có một số vấn đề về thiết kế với mã của bạn mà bạn có thể thu thập phản hồi ở đó. – Adam

Trả lời

1

Bạn có thể làm cho họ public static. Đây sẽ là một mã thô được tái cấu trúc để sửa lỗi biên dịch của bạn.

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; 
      } 
     } 
    } 

da da!

+1

Mã này sẽ khiếu nại khi yêu cầu một cá thể đối tượng trong dòng 'if (npc.notepad_running = 1)' vì nó được truy cập từ ngữ cảnh tĩnh. Cũng lỗi tương tự trên 'notepad_running = 1;' –

+0

Hoàn toàn đúng. Sửa lỗi đó. –

5

Bạn có thể làm điều đó bằng cách đặt thuộc tính public static trong notepad_check_class:

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

Tuy nhiên tôi sẽ đề nghị chỉ là một lớp:

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 cho giải pháp ** thực **. Một ghi chú nhỏ ở bên cạnh: 'Check()' có thể là 'private', và' Kill() 'có thể gọi' Check() 'trước khi làm bất cứ điều gì khác. – Adam

Các vấn đề liên quan