logo

I get negative index in array list (Visual Studio .NET)
mostafa hamdy posted at Thursday, November 20, 2008 6:08 AM

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

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0
re
Web star provided a rated reply to mostafa hamdy on Thursday, November 20, 2008 6: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

Reply    Reply Using Power Editor
  Rank Winnings Points
February 7 $39.00 48
January 7 $36.00 129


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos










  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010