質問

私は、私はそれに取り組んでいるプロジェクトは、重複を許可する「BaseSortedCollection」クラスを変更する必要があります。このクラスは、現在のIEnumerable、IDisposableを、ICollectionを、およびISerializableを実装しています。コレクションにアクセスする際のキーとして使用されているアイテムID(Int64型)を、持っている「BaseSortedCollection」店舗用アイテム。私は、同時にコレクションに存在するだけでなく、取得することができる二つの同一のアイテム(同じアイテムID)を持っている必要があります。

私たちは、2.0フレームワークを使用しています。

任意の提案ですか?

事前に感謝します!

役に立ちましたか?

解決

私はあなたが定期的にArrayListのを拡張し、あなたが自動ソートが必要な場合は、ソートを呼び出すには、Add-メソッドをオーバーライドする必要がありますね。しかし、私が?!

(ユニークどうあるべきか)、同じ2つの項目の考えのまわりで私の頭をラップするための識別番号を見えることはできません (System.Collections.Specialized中)

編集、または多分NameValueCollectionのがより適切でしょうか?それを拡張して、独自のソート方法を追加...

他のヒント

あなたは同じキーを持つ2つのアイテムを持っている場合、あなたはそのキーに対応するエントリのための2つのアイテムを含むリスト(T)を持つことになりますので、

あなたのBaseSortedCollectionの各項目は、リスト(T)可能性があります。

私はあなたがdouplicateキーを許可しない辞書の種類を拡張したことを想定しています。

これは何の実装について。私はあなたの項目はIComparableを実装すると仮定します。

class BaseSortedCollection<T> : Collection<T>, ICollection<T>, IEnumerable<T>,
    System.Collections.ICollection, System.Collections.IEnumerable
    where T : IComparable<T>
{
    /// <summary>
    ///     Adds an item to the Collection<T> at the correct position.
    /// </summary>
    /// <param name="item">The object to add to </param>
    public new void Add(T item)
    {
        int pos = GetInsertPositio(item);
        base.InsertItem(pos, item);
    }


    /// <summary>
    /// Convinience function to add variable number of items in one Functioncall
    /// </summary>
    /// <param name="itemsToBeAdded">The items to be added.</param>
    /// <returns>this to allow fluent interface</returns>
    public AutoSortCollection<T> AddItems(params T[] itemsToBeAdded)
    {
        foreach (var item in itemsToBeAdded)
            Add(item);
        return this;
    }

    /// <summary>
    /// Get position where item should be inserted.
    /// </summary>
    /// <param name="item"></param>
    /// <returns>Get position where item should be inserted.</returns>
    private int GetInsertPositio(T item)
    {
        if (item == null)
            throw new ArgumentNullException();

        for (int pos = this.Count - 1; pos >= 0; pos--)
        {
            if (item.CompareTo(this.Items[pos]) > 0)
                return pos + 1;
        }

        return 0;
    }
}

これは動作するはずです(MSTESTを使用して)

    /// <summary>
    ///A test sorting for SCCPackageEx Constructor
    ///</summary>
    [TestMethod()]
    public void SortingTest()
    {
        BaseSortedCollection<int> collection = new BaseSortedCollection<int>().AddItems(1,5,3,2,4,0);
        Assert.AreEqual(6, collection.Count, "collection.Count");

        for(int i=0; i <=5; i++)
           Assert.AreEqual(i, collection[i], "collection[" + i + "]");
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top