我想创建一个应用程序,显示当前打开的商店。每个商店都有名称,工作时间和类别。我制作了一个包含每个商店及其属性的列表,但是当我使用foreach方法时它不会显示任何一个......你碰巧知道问题是什么吗?或者以不同的方式解决它?
class Shop
{
public string name;
public DayOfWeek day{ get; set; }
public TimeSpan start{ get; set; }
public TimeSpan end{ get; set; }
public string address{ get; set; }
public string category{ get; set; }
public Shop(string name, DayOfWeek day, TimeSpan start, TimeSpan end, string address, string category)
{
name = this.name;
day = this.day;
start = this.start;
end = this.end;
address = this.address;
category = this.category;
}
}
public openshops()
{
List<Shop> openShop = new List<Shop>();
openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(18, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(12, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(19, 30, 0), new TimeSpan(00, 30, 0), "ermoupoli", "souvlaki"));
this.InitializeComponent();
DayOfWeek today = DateTime.Today.DayOfWeek;
TimeSpan now = DateTime.Now.TimeOfDay;
if (today == DayOfWeek.Friday)
{
foreach (Shop shop in openShop)
{
if ((now > shop.start) && (now < shop.end))
{
Button btn = new Button();
btn.Content = shop.name;
myStackPanel.Children.Add(btn);
}
}
}
}
答案 0 :(得分:8)
您正在反转构造函数中的设置逻辑:
你应该设置这样的成员:
public Shop(string name, DayOfWeek day, TimeSpan start, TimeSpan end, string address, string category)
{
this.name = name;
this.day = day;
this.start = start;
this.end = end;
this.address = address;
this.category = category;
}
答案 1 :(得分:0)
public Shop(string name, DayOfWeek day, TimeSpan start, TimeSpan end, string address, string category)
{
name = this.name;
day = this.day;
start = this.start;
end = this.end;
address = this.address;
category = this.category;
}
}
您正在为类中的未实例化变量设置参数值...
关键字
此
用于引用当前对象实例。
此外,按照惯例,属性名称应为Pascal大小写,因此如果您将代码重新格式化为:
class Shop
{
//public string name; //This one should have getters and setters too, fields shouldn't be exposed.
public string Name { get; set; }
public DayOfWeek Day{ get; set; }
public TimeSpan Start{ get; set; }
public TimeSpan End{ get; set; }
public string Address{ get; set; }
public string Category{ get; set; }
public Shop(string name, DayOfWeek day, TimeSpan start, TimeSpan end, string address, string category)
{
this.Name = name;
this.Day = day;
this.Start = start;
this.End = end;
this.Address = address;
this.Category = category;
}
}
一切都应该没问题。
关于惯例的参考: https://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.100%29.aspx
有关公开类字段的参考: http://thatextramile.be/blog/2009/10/slutty-types/
答案 2 :(得分:0)
除了修复设置属性的方式外,还有开始和结束时间的问题。
openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(18, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(12, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(19, 30, 0), new TimeSpan(00, 30, 0), "ermoupoli", "souvlaki"));
这些Shop
个对象的开始时间大于结束时间,因此if
语句
if ((now > shop.start) && (now < shop.end))
对于这些Shop
对象,始终为false。我猜你真的想交换那些价值。
openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(00, 0, 0), new TimeSpan(18, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(00, 0, 0), new TimeSpan(12, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(00, 30, 0), new TimeSpan(19, 30, 0), "ermoupoli", "souvlaki"));