Kat Dale
Kat Dale

Reputation: 27

List won't display it's content

I want to make an app that shows the shops that are currently open. Each shop has a name, work hours and a category. I made a list which contains each shop and its attributes, but when i use the foreach method it wont display any of them... Do you happen to know what the problem is ? or a different way to solve it?

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

            }
        }
    }

Upvotes: 1

Views: 79

Answers (3)

juharr
juharr

Reputation: 32276

In addition to fixing how you set your properties you also have an issue with the start and end times.

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"));

These Shop objects will have start times that are greater than the end times, so this if statement

if ((now > shop.start) && (now < shop.end))

Will always be false for these Shop objects. I'm guessing that you actually want to swap those values.

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"));

Upvotes: 0

kevinrodriguez-io
kevinrodriguez-io

Reputation: 1382

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

You are setting the parameters values to uninstantiated variables from your class...

the keyword

this

Is used to refer to the current object instance.

Also, by convention, property names should be Pascal case, so if you reformat your code to:

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

Everything should be ok.

Reference about convention: https://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.100%29.aspx

Reference about exposing class fields: http://thatextramile.be/blog/2009/10/slutty-types/

Upvotes: 0

Rodrigo L&#243;pez
Rodrigo L&#243;pez

Reputation: 4259

You are reversing the setting logic in your constructor:

You should set members like this:

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

Upvotes: 8

Related Questions