473,480 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HELP!! Why doesn't this code sort correctly?

Hi, I could use some help with this because I've run out of ideas.

I need to sort an arraylist containg objects of type TA, but so far no
luck.

Here's the code:

Class TAList: ArrayList {

normal arraylist methods...
.... add
.... remove

public TA Item(int i) {
if (_tacol.Count==0) {
throw new InvalidOperationException("Nothing available.");
} else if (i<=_tacol.Count) {
return((TA)_tacol[i]);
} else {
return(null);
}
}
}

Class TA {
public int ID = 0;
}

Class NTA: TA, IComparable {
private double m_STLI = 0;
public double STLI{
get{return(m_STLI);}
set{m_STLI =value;}
}

// returns:
// -1 if is less than x
// 0 if matches x
// 1 if is greater than x
public int CompareTo(object obj) {
if (obj is NTA) {
return this.m_STLI.CompareTo(((NTA)obj).STLI);
} else {
throw new ArgumentException("Object is not of type NTA");
}
}
}

class TAManager {
private TAList m_objTAList=null;
public TAList taList {
get {return(m_objTAList);}
set {m_objTAList=value;}
}
}

class NManager: TAManager {
public void LoadNTA() {
string sql = "select x,y,z from ta";
ArrayList al = DB.GetArrayList(DBType.SqlServer,sql);
foreach (object[] row in al) {
NTA nta = new NTA();
nta.ID = Convert.ToInt32(row[0]);
taList.Add(nta);
}
}

public double CalcLI() {

int icount = this.taList.Count;

if (icount>0) {

//input values
for (int i=0;i<icount;i++) {
NTA nta = (NTA)this.taList.Item(i);
nta.STLI = STLI();
}

taList.Sort(); //<--- FAILS HERE!!!

}

}
}

I get the following error message:
"Offset and length were out of bounds for the array or count is greater
than the number of elements from index to the end of the source
collection."

but I can't find what is it that I'm doing wrong. Please help !

Thank you

Nov 17 '05 #1
2 1140
Hi Batatoon,
At first glance your exception could happen here:

public TA Item(int i) {
...
} else if (i<=_tacol.Count) {
return((TA)_tacol[i]);
...
}

if _tacol is a zero based index then you should test for less than the Count
property instead of less than or equal to. The count could be 15, but the
object at the end would only have an index of 14.

Cheers,
Steve Goodyear,
Vancouver Canada
Nov 17 '05 #2

Changed it, but I still get the same error. :(

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2491
by: Don Seckler | last post by:
I am trying to set up a PHP web page so I can enter data into a database. I created a form: <form action="admin_news_insert_processor.php" method="post" name="frm_Insert_News"...
0
303
by: Dale McGorman | last post by:
The following is some code that I am trying to bring over from VB 6.0, that I have working there. I am trying to get to where I can talk to a USB device. I am stuck on how to correctly pass params...
3
2008
by: JGBNS via DotNetMonster.com | last post by:
Hi, I am new to this forumand I apologize as i am not a .net programmer but we have a program being developed by a .net programmer. Nowwe have run into an ftp snag and I think it is part ftp and...
12
1884
by: Tom Wright | last post by:
Hi all I'm using the following code with Apache and mod-xslt but it's not sorting the elements as I want it to. At the moment, it sorts them with all items starting with an uppercase letter...
0
1199
by: StephDawg40 | last post by:
I am attempting to write a Quicksort algorithm and my partition function is not behaving correctly. here is the code int CDataAnalyzerDlg::partition1(int first, int last) { int pivot =...
2
3957
by: gonzo | last post by:
So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the...
2
1153
by: gonzocanuck | last post by:
Hi guys, I need some help performing a sort. I tried searching Google and Google Groups, but maybe I'm asking the wrong question or using the wrong terminology, so I'll explain it. I have a...
5
3878
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
2
1305
by: Ty | last post by:
The top block is the original code (I have already tried automatic convertors but they error everytime.) and the second block is what I have done. At the end will be the line that has me stumped. ...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7003
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...
0
5357
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,...
0
4495
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...
0
3008
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...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
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 ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
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...

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.