I get negative index in array list

Asked By mostafa hamdy
20-Nov-08 06:08 AM
Earn up to 0 extra points for answering this tough question.

hello all

I use asp.net 2 and I have some problem with array list , I have some array contains some objects and when I use the Binary seach method of the array list I get negative index althogh this object already exist in the array list the code I wrote is as the following ,please if any body can help me or can tell me about some URL can help me in solving this problem please send me

ArrayList arrOverTime = GetLengthOfOverTimePeriods(dtEmployeeOverTime);

int Index = 0;

while (DateTime.Parse(DateTime.Parse(MonthStartDay).ToShortDateString()) <= DateTime.Parse(DateTime.Parse(MonthEndDay).ToShortDateString()))

{

//compare every day in the month , is it in the arrovertime

object objDate = new object();

objDate = MonthStartDay;

Index = arrOverTime.BinarySearch(objDate);//is + if item exit in the array or - if not exit

if (Index < 0)

// in case the index is negative it means that this day is not in the over time period so update its record if any, with the new time rule

{

//updat the current month records with the new time rule

objCheckInOut.UpdateEmployeeCheckInOutRecordForOverTime(ContactID, DateTime.Parse(MonthStartDay), DateTime.Parse(DateTime.Parse(MonthStartDay).ToShortDateString()+" "+"23:59:59.000"), int.Parse(ddlTimeRule.SelectedValue));

}

MonthStartDay = DateTime.Parse(MonthStartDay).AddDays(1).ToShortDateString();

}

regards

Mostafa

  re

Web Star replied to mostafa hamdy
20-Nov-08 06:16 AM

The BinarySearch member of the ArrayList was very useful for me. This member returns the zero-based index of a specific element in the sorted ArrayList or a portion of it, using a binary search algorithm.

int myIndex=myList.BinarySearch( 3 );

That's it..  The ArrayList class is much more fun than this. See documentation for more details.

http://www.c-sharpcorner.com/UploadFile/mahesh/b_search11262005012607AM/b_search.aspx

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.binarysearch.aspx

bytes.com/forum/thread231456.html

Create New Account