473,890 Members | 1,815 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 1426
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
2664
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
2061
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
1469
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
2022
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
2008
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
1947
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
1755
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
9803
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
10058
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
11214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10903
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
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
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...
0
7158
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
5834
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
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4659
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.