As mentioned above, to create a collection, you can derive it from the IList interface. Here is an example: #pragma once using namespace System::Collections; public ref class CBookList : public IList { }; This System::Collections::IList interface is declared as follows: public interface class IList : ICollection, IEnumerable This System::Collections::Generic::IList interface is declared as follows: generic<typename T> public interface class IList : ICollection<T>, IEnumerable<T>, IEnumerable This means that the IList interface extends both the ICollection and the IEnumerable interfaces. This also implies that you must implement the members of these parent interfaces. In other words, you must implement the Count property, the SyncRoot property, the IsSynchronized property, and the CopyTo() method of the ICollection interface. From what we learned with ICollection, here are examples of implementing these members for the System::Collections::IList interface: Header File: BookList.h #pragma once using namespace System; using namespace System::Collections; public ref class CBookList : public IList { private: int counter; array<Object ^> ^ objs; public: CBookList(void); virtual property int Count { int get() { return counter; } } virtual property bool IsSynchronized { bool get() { return false; } } virtual property Object ^ SyncRoot { Object ^ get() { return this; } } virtual void CopyTo(Array ^ ary, int index); }; Source File: BookList.cpp #include "BookList.h" CBookList::CBookList(void) : counter(0), objs(gcnew array<Object ^>(5)) { } void CBookList::CopyTo(Array ^ ary, int index) { } You must also implement the System::Collections::GetEnumerator() (or the System::Collections::Generic::GetEnumerator()) method of the System::Collections::IEnumerable (or of the System::Collections::Generic::IEnumerable) interface. If you don't have time to completely implement it, you can simply return nullptr. Here is an example for the System::Collections::IList interface: Header File: BookList.h #pragma once using namespace System; using namespace System::Collections; public ref class CBookList : public IList { private: int counter; array<Object ^> ^ objs; public: CBookList(void); virtual property int Count { int get() { return counter; } } virtual property bool IsSynchronized { bool get() { return false; } } virtual property Object ^ SyncRoot { Object ^ get() { return this; } } virtual void CopyTo(Array ^ ary, int index); virtual IEnumerator ^ GetEnumerator(); }; Source File: BookList.cpp #include "BookList.h" CBookList::CBookList(void) : counter(0), objs(gcnew array<Object ^>(5)) { } void CBookList::CopyTo(Array ^ ary, int index) { } IEnumerator ^ CBookList::GetEnumerator() { return nullptr; }
|
|
|||||||
|