提问人:Thomas Eikje Gjerde 提问时间:12/28/2022 最后编辑:Hossein SabzianiThomas Eikje Gjerde 更新时间:1/2/2023 访问量:84
C# 在方法中调用方法
C# Calling methods in methods
问:
我有一个代码,可以在单击按钮时计算一些东西。计算公式将根据选中的单选按钮而有所不同。某些计算输入将保持不变,与检查的单选按钮无关。所以我想做的是创建一个方法,该方法将在两个不同的方法(案例 1 和案例 2)中调用,但是在案例 1 和 2 的方法中调用时,我无法访问 calculationInput 中定义的变量,我得到“名称在当前上下文中不存在”。谁能帮忙?
有关代码示例,请参见下文:
// Calculation input that will stay the same for each case
private void calculationInput(oject sender, EventArgs e)
{
double a = 100;
double b = 200;
double c = 300;
}
//Case 1
private void calculationCaseOne(object sender, EventArgs e)
{
calculationInput(new object(), new EventArgs());
double case1 = a+b;
label.Text=case1.ToString();
}
//Case 2
private void calculationCaseTwo(object sender, EventArgs e)
{
calculationInput(new object(), new EventArgs());
double case2 = a+c;
label.Text=case2.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
calculationCaseOne(new object(), new EventArgs());
}
if (radioButton2.Checked == true)
{
calculationCaseTwo(new object(), new EventArgs());
}
}
答:
3赞
Elie Asmar
12/28/2022
#1
在当前实现中,变量 a、b 和 c 是在 calculationInput 方法中定义的局部变量,因此无法从该方法外部访问它们。
您需要将它们定义为类的字段,而不是方法中的局部变量。
public partial class Form1 : Form
{
// Fields that will hold the calculation input
private double a;
private double b;
private double c;
// Method that initializes the calculation input
private void calculationInput()
{
a = 100;
b = 200;
c = 300;
}
// Case 1
private void calculationCaseOne()
{
calculationInput();
double case1 = a + b;
label.Text = case1.ToString();
}
// Case 2
private void calculationCaseTwo()
{
calculationInput();
double case2 = a + c;
label.Text = case2.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
calculationCaseOne();
}
if (radioButton2.Checked == true)
{
calculationCaseTwo();
}
}
}
评论
1赞
Olivier Jacot-Descombes
12/28/2022
可以丢弃,只需用 .-condition 需要 a,属性生成 .== true
if (radioButton1.Checked)
If
bool
Checked
bool
0赞
Elie Asmar
12/29/2022
是的,事实上,我选择故意将其编写为编码约定。
0赞
OJ Raqueño
12/28/2022
#2
最好在方法之外声明这些值。
double a = 100;
double b = 200;
double c = 300;
private void calculationCaseOne(object sender, EventArgs e)
{
/* a, b, and c can be accessed here */
}
private void calculationCaseTwo(object sender, EventArgs e)
{
/* a, b, and c can be accessed here */
}
private void button1_Click(object sender, EventArgs e) { /* ... */ }
2赞
Hossein Sabziani
12/28/2022
#3
的 ,只需定义变量calculationInput function is not needed
/* private void calculationInput(oject sender, EventArgs e)
{
double a = 100;
double b = 200;
double c = 300;
}*/
private double a = 100;
private double b = 200;
private double c = 300;
0赞
Narish
12/28/2022
#4
其他答案是将这些字段设置为私有字段,但从我在 button1_Click 方法中看到的内容来看,您需要这些值从 calculationInput() 中指定的值开始
出于这个原因,您应该将这些字段声明为,在这种情况下,它们始终是该值并在编译时内联,或者在这种情况下,它们可以内联或在构造函数级别初始化(现在不是必需的,但在以后的重构中,为在构造中注入所需的值开辟了可能性)const
readonly
Const 版本,在代码执行过程中的任何时候都不能更改:
private const double _a = 100;
private const double _b = 200;
private const double _c = 300;
只读,在 ctor 调用后无法更改:
//init here
private readonly double _a;
private readonly double _b;
private readonly double _c;
//*OR* you init them here, in which case you can perform logic on them
public My Form()
{
_a = 100;
_b = 200;
_c = 300;
/*
//an example of injecting in values
if (boolMeaningSomething)
{
//set to default -> 0.0
_a = default(double);
_b = default(double);
_c = default(double);
}
else
{
//set to your valuues
_a = 100;
_b = 200;
_c = 300;
}
*/
}
评论
calculationInput()
a
b