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

B

batatoon

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);
} 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
 
G

Guest

Hi Batatoon,
At first glance your exception could happen here:

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top