469,934 Members | 2,049 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Help with ArrayList.BinarySearh

What I want to be able to do is create an indexer that can index into an
ArrayList filled with objects of my own type. I have the following class
structure:

----------------------------------------------
class PropertyList : ArrayList {
public enum SortOrder { }

public object this [string name] { // cannot figure this one
t }

public void Sort (SortOrder order) { ... base.Sort
(oPropertyListSort); ... }

class PropertyListSort : IComparer {
SortOrder order;
public SortOrder SortField { }
public int Compare (object x, object y)
}
}
class ADOProperty { }
----------------------------------------------

The PropertyList contains a bunch of ADOProperty objects. The Sort routine
can sort the objects based on 2 different fields. This routine works fine.

What I cannot do is figure out how to write the indexer so I can get the
object based on the value of the sorted field. It seems that I should
somehow be able to call this.BinarySearch inside of the indexer to find the
desired object but I cannot find a set of arguments that will not cause an
'System.InvalidOperationException'

Is there a better or more standard way to attack this problem. I can think
of more involved ways to go about achieving my goal but but this seems like
something that the ArrayList should be able to handle (and now I'm
stubborn).

Thanks,
bob
Nov 15 '05 #1
4 1249
Bob,

I personlly haven't inherited from ArrayList. Instead I use Collectionbase
so that I can strongly type the objects being added. With that said, I use
method to return the index of a object matching a paramter. For example:

public Person IndexOfPerson( string strName, int intStartIdx )
{
int intIdxOut = -1;
Person objPerson;

for( int intIdx = intStartIdx;
intIdx < this.Length;
intIdx++ )
{
objPerson = ( Person )this[ intIdx ];

if ( objPerson.Name == strName )
{
intIdxOut = intIdx;
}
}
}

Keep in mind you can encapsilate any kind of search you want to. If you
really want an indexer you could write your own but Normally there is only
one indexer and multipule methods to find specific objects.

Hope this helps.

--
Glen Jones MCSD

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...
What I want to be able to do is create an indexer that can index into an
ArrayList filled with objects of my own type. I have the following class
structure:

----------------------------------------------
class PropertyList : ArrayList {
public enum SortOrder { }

public object this [string name] { // cannot figure this one
t }

public void Sort (SortOrder order) { ... base.Sort
(oPropertyListSort); ... }

class PropertyListSort : IComparer {
SortOrder order;
public SortOrder SortField { }
public int Compare (object x, object y)
}
}
class ADOProperty { }
----------------------------------------------

The PropertyList contains a bunch of ADOProperty objects. The Sort routine can sort the objects based on 2 different fields. This routine works fine.
What I cannot do is figure out how to write the indexer so I can get the
object based on the value of the sorted field. It seems that I should
somehow be able to call this.BinarySearch inside of the indexer to find the desired object but I cannot find a set of arguments that will not cause an
'System.InvalidOperationException'

Is there a better or more standard way to attack this problem. I can think of more involved ways to go about achieving my goal but but this seems like something that the ArrayList should be able to handle (and now I'm
stubborn).

Thanks,
bob

Nov 15 '05 #2
I am new to C# and .Net and haven't looked at the Collectionbase yet. I'm
looking at it in my C# in a Netshell book (my new bible) and it is a curious
class. It contains an ArrayList and has a bunch of OnSuchandSuch methods
which look like they were meant to be event handlers in the derived class.
Strongly typed additions is a good thing but I don't see how the class helps
enforce that.

I will look into this class more. Thank you for replying.
bob
"Glen Jones MCSD" <gl********@mailhot.com> wrote in message
news:DJ********************@comcast.com...
Bob,

I personlly haven't inherited from ArrayList. Instead I use Collectionbase so that I can strongly type the objects being added. With that said, I use
method to return the index of a object matching a paramter. For example:

public Person IndexOfPerson( string strName, int intStartIdx )
{
int intIdxOut = -1;
Person objPerson;

for( int intIdx = intStartIdx;
intIdx < this.Length;
intIdx++ )
{
objPerson = ( Person )this[ intIdx ];

if ( objPerson.Name == strName )
{
intIdxOut = intIdx;
}
}
}

Keep in mind you can encapsilate any kind of search you want to. If you
really want an indexer you could write your own but Normally there is only
one indexer and multipule methods to find specific objects.

Hope this helps.

--
Glen Jones MCSD

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...
What I want to be able to do is create an indexer that can index into an
ArrayList filled with objects of my own type. I have the following class structure:

----------------------------------------------
class PropertyList : ArrayList {
public enum SortOrder { }

public object this [string name] { // cannot figure this one
t }

public void Sort (SortOrder order) { ... base.Sort
(oPropertyListSort); ... }

class PropertyListSort : IComparer {
SortOrder order;
public SortOrder SortField { }
public int Compare (object x, object y)
}
}
class ADOProperty { }
----------------------------------------------

The PropertyList contains a bunch of ADOProperty objects. The Sort

routine
can sort the objects based on 2 different fields. This routine works

fine.

What I cannot do is figure out how to write the indexer so I can get the
object based on the value of the sorted field. It seems that I should
somehow be able to call this.BinarySearch inside of the indexer to find

the
desired object but I cannot find a set of arguments that will not cause an 'System.InvalidOperationException'

Is there a better or more standard way to attack this problem. I can

think
of more involved ways to go about achieving my goal but but this seems

like
something that the ArrayList should be able to handle (and now I'm
stubborn).

Thanks,
bob


Nov 15 '05 #3
Hello Bob,

Thanks for posting in the group.

Based on my understanding, now the problem is: You have a ArrayList whose
elements are objects of your own type. However, it is hard to create an
indexer for the array. Please feel free to post here if I have
misunderstood anything.

Glen has provided a good method for you already. On my opinion, you can
also think of inheriting your class from SortedList class. A SortedList is
a hybrid between a Hashtable and an Array. When an element is accessed by
its key using the Item indexer property, it behaves like a Hashtable. When
an element is accessed by its index using GetByIndex or SetByIndex, it
behaves like an Array.

Does that answer your question? If you have any more questions on it,
please feel free to post in the group.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 15 '05 #4
Hello Bob,

I wanted to post a quick note to see if you would like additional
assistance or information regarding this particular issue. We appreciate
your patience and look forward to hearing from you!

Thanks for participating the community.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Marc Bishop | last post: by
6 posts views Thread by GrandpaB | last post: by
blazedaces
6 posts views Thread by blazedaces | last post: by
3 posts views Thread by carlos123 | last post: by
2 posts views Thread by hcaptech | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.