473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ 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 InvalidOperatio nException("Not hing available.");
} else if (i<=_tacol.Coun t) {
return((TA)_tac ol[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_ST LI);}
set{m_STLI =value;}
}

// returns:
// -1 if is less than x
// 0 if matches x
// 1 if is greater than x
public int CompareTo(objec t obj) {
if (obj is NTA) {
return this.m_STLI.Com pareTo(((NTA)ob j).STLI);
} else {
throw new ArgumentExcepti on("Object is not of type NTA");
}
}
}

class TAManager {
private TAList m_objTAList=nul l;
public TAList taList {
get {return(m_objTA List);}
set {m_objTAList=va lue;}
}
}

class NManager: TAManager {
public void LoadNTA() {
string sql = "select x,y,z from ta";
ArrayList al = DB.GetArrayList (DBType.SqlServ er,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.Cou nt;

if (icount>0) {

//input values
for (int i=0;i<icount;i+ +) {
NTA nta = (NTA)this.taLis t.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 1152
Hi Batatoon,
At first glance your exception could happen here:

public TA Item(int i) {
...
} else if (i<=_tacol.Coun t) {
return((TA)_tac ol[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
2493
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" id="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 to the API call SetupDiGetDeviceInterfaceDetail() I can get it to actully not fail, and it actully tells me that it returned 87 bytes which is...
3
2027
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 part .net coding. Heres the issue.. We have an image directory full of photos like 200,000 in it, and if you use an ftp client to ftp to the...
12
1893
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 first, like this: A B C a b c no matter what the setting of the case-order attribute is. It rather looks like it's sorting them by ASCII (or...
0
1205
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 = myVector; int left = first; int right = last; swap(myVector, myVector);
2
3965
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 strings can be sorted. My problem is kind of hard to understand, so please bear with me. I've figured out how to sort the last names, first names and birth...
2
1159
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 database of a list of words, such as: horse dog
5
3891
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 no path included (no C:\...) eg: my_file2.jpg, my_file1.bmp, etc... and m_path stores the path, eg: C:\folder1 I want to sort this vector...
2
1308
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. Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope, Optional $User,Optional $Pswd) Dim...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
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...
1
7461
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...
0
7792
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...
0
6026
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...
0
5080
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...
0
3491
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...
1
1921
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
1
1046
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.