Friday 15 March 2013

ArrayList.IndexOf returns -1 in c# -



ArrayList.IndexOf returns -1 in c# -

please confused. have arraylist size of 184 . among list 25

[1] = 25

[2] = 30 etc.

i trying homecoming index of item example. returns 0 item 25 returns 1 30. however, ever item used . when used

int l = myarray.indexof(myitem) ;

it returns -1. item 30. replace myitem 30

int l = myarray.indexof(30) ; int l = myarray.indexof("30") ;

both returns -1. however, when tried

int l = convert.toint16( myarray[2]);

the above returns 30. please did go wrong ? first time encounter this. confused. inturn tried.

int l = myarray.indexof("30", 1) ; int l = myarray.indexof(30 ,1) ;

yet nil helps .

update

thank time.

i had utilize list suggested user below. give thanks 1 time 1 time again man. if write ur reply , take perhaps may help 1 . modify code below. works , not sure why listarray fails.

list<long> li = new list<long>(); foreach (long item in arlclientid) li.add(item); int h = li.indexof(searchduplicate.clientid);

the reason @blorgbeard suggested utilize list instead of arraylist because arraylist non-generic collection , when add together item arraylist accepts object rather int\string etc. more info here.

the reason suspect getting -1 output 30 not integer in "myarray" string described below:-

arraylist myarray = new arraylist(); myarray.add(25); myarray.add(50); myarray.add("30"); console.writeline(myarray.indexof(30)); // returns -1 console.writeline(myarray.indexof("30")); // returns 2

so, much improve , safe utilize generic list collection rather arraylist.

c# arraylist

No comments:

Post a Comment