如何在上面的类中使变量 cRentStart 可以访问我程序中的所有类?
目前我在form1初始化时使用 dateCheck ,所以我想保留它,然后继续在另一个名为 private void viewOverdue_Click <的事件中使用它/强>
public Form1()
{
InitializeComponent();
viewRent.ForeColor = Color.Red;
dateCheck();
}
void dateCheck()
{
CurrentDate.Text = "" + DateTime.Now;
DateTime cRentStart, cRentEnd;
DateTime today = DateTime.Now;
if (today.DayOfWeek == DayOfWeek.Monday)
{
cRentStart = today.AddDays(-5);
cRentEnd = today.AddDays(2);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Tuesday)
{
cRentStart = today.AddDays(-6);
cRentEnd = today.AddDays(1);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Wednesday)
{
cRentStart = today.AddDays(0);
cRentEnd = today.AddDays(7);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Thursday)
{
cRentStart = today.AddDays(-1);
cRentEnd = today.AddDays(6);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Friday)
{
cRentStart = today.AddDays(-2);
cRentEnd = today.AddDays(5);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Saturday)
{
cRentStart = today.AddDays(-3);
cRentEnd = today.AddDays(4);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
else if (today.DayOfWeek == DayOfWeek.Sunday)
{
cRentStart = today.AddDays(-4);
cRentEnd = today.AddDays(3);
CurrentRent.Text = "Current Rent Week: " + cRentStart.ToString("dd/MM/yyyy") + " - " + cRentEnd.ToString("dd/MM/yyyy");
}
}
答案 0 :(得分:1)
您想要的是全局变量 请参阅全局变量this page。
一些注意事项:
示例
namespace MyApp
{
public class MyClass
{
public static string MyString { get; set; }
public MyClass()
{
}
}
public class MyOtherClass
{
public MyOtherClass()
{
MyClass.MyString = "Test";
}
}
}
答案 1 :(得分:0)
给它正确的范围...... http://msdn.microsoft.com/en-us/library/ms973875.aspx
目前,cRentStart的范围仅限于dateCheck方法。使用正确的访问器在该范围之外声明它,以便从其他地方访问它。