在类和方法之间共享变量

时间:2011-12-30 01:41:39

标签: c# class variables methods

我写了这个,我得到以下错误。有没有简单的方法让变量看到对方?

  

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

3 个答案:

答案 0 :(得分: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() { ... }

}

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分: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!