473,705 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with ArrayList.Binar ySearh

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
(oPropertyListS ort); ... }

class PropertyListSor t : 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.BinarySear ch inside of the indexer to find the
desired object but I cannot find a set of arguments that will not cause an
'System.Invalid OperationExcept ion'

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 1408
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******** ******@TK2MSFTN GP12.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
(oPropertyListS ort); ... }

class PropertyListSor t : 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.BinarySear ch inside of the indexer to find the desired object but I cannot find a set of arguments that will not cause an
'System.Invalid OperationExcept ion'

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********@mai lhot.com> wrote in message
news:DJ******** ************@co mcast.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******** ******@TK2MSFTN GP12.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
(oPropertyListS ort); ... }

class PropertyListSor t : 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.BinarySear ch inside of the indexer to find

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2657
by: almurph | last post by:
Hi, Hope I have the right forum here - apologies if I don't. I'm trying to access a ArrayList collection via an indexer inside a class called "collExample" What I want to be able to do is to store and retrieve *any* type of object to the ArrayList. However I am getting conversion errors - Can anyone helpe me please?
5
2053
by: Marc Bishop | last post by:
I'm trying to get this to work but i'm at a loss as to how. i've searched google without much help code Dim ArrCart As new ArrayList() ArrCart = CType(Session("sesCart"), ArrayList) ArrCart.add(sID & ",1") Session("sesCart") = ArrCart So what i want to do is maintain my Arraylist of items in a session.
6
1456
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB example I found on the web. I would be most appreciative if someone could explain why this statement is necessary and what does it do: MyArt = New Art
16
2007
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
2
1999
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some reason now it is not finding ones that are single posts. Here is an example of a header for a single. (Ask the Dust ) - "atd-ftc-repack.nfo" www.ctjes.com (1/1) (1/1) at the end means it is part 1 of a 1 part post. Any help would be...
6
1942
blazedaces
by: blazedaces | last post by:
Hey, so here's an example of what I want to do: This "window" if you will, or simple gui, will accept an ArrayList<String> and in turn spit out for every one of them a checkbox and a text field where you can rename it to whatever name you chose (later I'll add something to check and make sure none of the names are the same... but for now it doesn't matter). I'm not very familiar with GUI in java so I don't know the best way of doing this. ...
3
1746
by: carlos123 | last post by:
Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article. Would someone just take a look at this code, i know it sucks. Look at the areas that say "HELP!!!" next to them. But basicly i want to take data from textfields and put them into an arraylist. So the user inputs data, name, address, age all that, hits save (This is when it goes into the arraylist) the textfields clear, and they are ready...
12
9778
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath As String End Structure
2
10033
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
8771
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9140
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5934
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2084
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.