On Thu, Jul 15, 2010 at 9:27 AM, Shipley, Peter G.
<Peter.Shipley@xxxxxxxxxx> wrote:
Hello Everyone,
I have been given the task of rewriting a Visual RPG Shop Floor system in VB.Net.
Would anyone be able to advise me on how I can use an Array to lookup a date in VB.Net?
Peter,
( VB instead of C# ?)
the .net framework way to search any sequence of items ( the
collection implements the IEnumerable interface ) is to use a lambda
expression.
DateTime foundDate = dateList.FirstOrDefault(c => c >= searchDttm);
FindFirstOrDefault is the equivalent of Lookup.
The "c" in the lambda represents the item in the collection. If the
collection held rows from a database the lambda would read: c =>
c.OrderDate >= searchDttm.
The default value for an object is null. Since DateTime is a value
type, the default value is DateTime.MinValue ( how the method knows
that, I am not sure ;) )
here is some c# code that uses a lambda expression to find the first
date in a list that is >= the search date:
void DateLookup()
{
DateTime curDttm = DateTime.Now;
List<DateTime> dateList = new List<DateTime>();
for (int cx = 0; cx < 5; cx++)
{
dateList.Add(curDttm);
curDttm += new TimeSpan(7, 0, 0, 0); // 7 days
}
DateTime searchDttm = DateTime.Now + new TimeSpan(12, 0, 0, 0);
DateTime foundDate = dateList.FirstOrDefault(c => c >= searchDttm);
if (foundDate == DateTime.MinValue)
MessageBox.Show("date not found");
else
MessageBox.Show("found date " + foundDate.ToString());
}
-Steve
As an Amazon Associate we earn from qualifying purchases.