Jeremy
Jeremy

Reputation: 447

How can I see what time is closest to one is my list?

I'm developing an app for my mobile phone where you can see bus times you enter a time with a listbox and then it checks the list and then presents you the time closest to it or an exact match. I Have tried myself and had most luck with some code I found on here yet i'm still unable to get it to work.

Here

    public  MainPage()
    {
        InitializeComponent();

        List<string> thedates = new List<string>();



        thedates.Add("0130");
        thedates.Add("0230");
        thedates.Add("0330");
        thedates.Add("0430");
        thedates.Add("0530");


        DateTime fileDate, closestDate;


        int min = int.MaxValue;

        foreach (DateTime date in theDates)
            if (Math.Abs(date.Ticks - fileDate.Ticks) < min)
            {
                min = date.Ticks - fileDate.Ticks;
                closestDate = date;
            }
    }

Error: The name 'theDates' does not exist in the current context.

Sorry if this is something simple or complicated. Any help is appreciated.

Upvotes: 0

Views: 157

Answers (4)

Max
Max

Reputation: 13338

This is a very basic error and should be found almost everywhere on the internet, the problem is that you are searching with your foreach loop in a list called theDates, that doesn't exists in your application.

You declared the list: thedates at the top of your application and you want to use theDates, you could rename thedates to theDates or just change theDates to thedates in your foreach loop.

You should also change your List<string> to a List<DateTime>.

The list<DateTime> will be filled as follow:

theDates.Add(today.AddHours(13).AddMinutes(30));
theDates.Add(today.AddHours(14).AddMinutes(30));
theDates.Add(today.AddHours(15).AddMinutes(30));
theDates.Add(today.AddHours(16).AddMinutes(30));
theDates.Add(today.AddHours(17).AddMinutes(30));

Remember: c# is a case sensitive language.

Upvotes: 2

evanmcdonnal
evanmcdonnal

Reputation: 48076

I think the cleanest way is to do a simple LINQ query. I just moved your ticks math into the select statement then tack on a call to Min() to get the smallest element in the result set.

  closetDate = myDates.Select(Math.Abs(x => x.Ticks - fileDate.Ticks)).Min() 

Upvotes: 1

Sunny Agrawal
Sunny Agrawal

Reputation: 102

  1. You are creating a list "thedates" and in foreach is working on "theDates", Variables are case sensitive
  2. after you change anyone of the both, still there will be problems as your thedates container is a list of strings and your foreach loop is expecting a container containing Datetime Objects

Upvotes: 2

Jon La Marr
Jon La Marr

Reputation: 1398

Change 'theDates' in

foreach (DateTime date in theDates)

to 'thedates'.

As mentioned--you also aren't using the correct objects. You should just create a List of DateTime objects instead of strings.

List<DateTime> thedates = new List<DateTime>();

thedates.Add(new DateTime{ // Set up values here });
..
..

Upvotes: 5

Related Questions