M
majm
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the generic.List.IndexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.
The contained class (IssStruct) implements the IComparable and
IComparable<T> interfaces.
However, the List.Sort function does invoke CompareTo, which suggests
that the syntax for the IComparable interface is correct. The same
problem occurs when the class in contained in an ArrayList.
I'm a rank beginner, so feel free to point out anything obvious.
Below are the class declarations. IssStructs is a generic list that
contains IssStruct class instances; UnsafeIssStructs is an ArrayList
that contains IssStruct class instances. They behave the same. IndexOf
does not invoke IssStruct.CompareTo, Sort does invoke
IssStruct.CompareTo.
Muchos thanks to anyone who can offer help.
namespace ClassLibrary1
{
// this is the contained class
public class IssStruct:IComparable<IssStruct>, IComparable
{
public string fStructName;
public IssStruct(string iStructName)
{
fStructName = iStructName;
}
// non-generic version of CompareTo
public int CompareTo(object obj)
{
return fStructName.CompareTo(((IssStruct)obj).fStructName);
}
// generic version of CompareTo
public int CompareTo(IssStruct obj)
{
return fStructName.CompareTo(obj.fStructName);
}
} // end IssStruct, the contained class
// container class. "Has a..." list, not an "Is a..." list
public class IssStructList
{
public List<IssStruct> IssStructs;
public ArrayList UnSafeIssStructs;
public IssStructList()
{
IssStructs = new List<IssStruct>();
UnSafeIssStructs = new ArrayList();
}
}
} // end namespace ClassLibrary1
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the generic.List.IndexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.
The contained class (IssStruct) implements the IComparable and
IComparable<T> interfaces.
However, the List.Sort function does invoke CompareTo, which suggests
that the syntax for the IComparable interface is correct. The same
problem occurs when the class in contained in an ArrayList.
I'm a rank beginner, so feel free to point out anything obvious.
Below are the class declarations. IssStructs is a generic list that
contains IssStruct class instances; UnsafeIssStructs is an ArrayList
that contains IssStruct class instances. They behave the same. IndexOf
does not invoke IssStruct.CompareTo, Sort does invoke
IssStruct.CompareTo.
Muchos thanks to anyone who can offer help.
namespace ClassLibrary1
{
// this is the contained class
public class IssStruct:IComparable<IssStruct>, IComparable
{
public string fStructName;
public IssStruct(string iStructName)
{
fStructName = iStructName;
}
// non-generic version of CompareTo
public int CompareTo(object obj)
{
return fStructName.CompareTo(((IssStruct)obj).fStructName);
}
// generic version of CompareTo
public int CompareTo(IssStruct obj)
{
return fStructName.CompareTo(obj.fStructName);
}
} // end IssStruct, the contained class
// container class. "Has a..." list, not an "Is a..." list
public class IssStructList
{
public List<IssStruct> IssStructs;
public ArrayList UnSafeIssStructs;
public IssStructList()
{
IssStructs = new List<IssStruct>();
UnSafeIssStructs = new ArrayList();
}
}
} // end namespace ClassLibrary1