gpt4 book ai didi

c# - 在 C# 作用域外声明变量

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:52 25 4
gpt4 key购买 nike

真是个愚蠢的问题,因为我认为你做不到,但是

 namespace Parking_Ticket_Fines
{
public partial class frmParking : Form
{
public frmParking()
{
InitializeComponent();
}

private void btnCal_Click(object sender, EventArgs e)
{
//Variable
int Total = 0;

//Checks which radio button is checked
if (radExpired.Checked)
{
Total = 35;
}
else if (radParking.Checked)
{
Total = 75;
}
else if (radDriveway.Checked)
{
Total = 150;
}
else if (radHandicap.Checked)
{
Total = 500;
}
}

private void btnClear_Click(object sender, EventArgs e)
{
//Clears everything
lblTotal.Text = "";
radRepeat.Checked = false;
radExpired.Checked = true;
}

有没有一种方法可以在两个私有(private)空隙中使用“Total”变量而无需声明两次?

最佳答案

Is there a way I can use the "Total" variable in both private voids without declaring it in twice?

是的,在类级别声明它。

public partial class frmParking : Form
{
private int Total; //Here
public frmParking()
{
InitializeComponent();
}

Total 将是一个字段阅读更多关于 Fields (C# Programming Guide)

关于c# - 在 C# 作用域外声明变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833129/

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