C# – Share variables between classes and methods

cclassmethodsvariables

I wrote this and I get the following errors. Is there any simple way to make the variables see each others?

Warning 1 The variable 'notepad_running' is assigned but its value is never used.

Error 2 The name 'notepad_running' does not exist in the current context.

Error 3 The name 'notepad_list' does not exist in the current context.

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

Best Answer

You could do it by putting public static properties in notepad_check_class:

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

However I would suggest just one class:

public static class NotepadManager {

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

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

}