gpt4 book ai didi

c# - 在类和方法之间共享变量

转载 作者:太空狗 更新时间:2023-10-29 23:10:04 27 4
gpt4 key购买 nike

我写了这篇文章,但出现了以下错误。有什么简单的方法可以让变量相互看到吗?

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

最佳答案

您可以通过将公共(public)静态属性放在 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() { ... }

}

关于c# - 在类和方法之间共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com