A
Alexander Widera
Hi,
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?
Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;
public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new Hashtable());
protected int _Version = 1;
// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}
// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}
bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}
object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}
void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}
public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}
public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}
public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;
public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}
private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}
public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}
object IEnumerator.Current {
get { return(Current); }
}
public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}
public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?
Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;
public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new Hashtable());
protected int _Version = 1;
// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}
// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}
bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}
object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}
void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}
public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}
public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}
public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;
public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}
private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}
public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}
object IEnumerator.Current {
get { return(Current); }
}
public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}
public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}