Home

C++/CLI Collections:
The IList Interface

 

Introduction

While it provides the minimum functionality of a collection, the ICollection interface is not equipped to perform the regular operations of a collection class, such as adding, retrieving, or deleting items from a set. To assist you with creating a class as complete as possible, the .NET Framework provides an interface named IList. The IList interface, defined in the System::Collections namespace, is equipped with the methods necessary to add, insert, delete, or retrieve items from a collection. Because the functionalities of these methods may not suit you, to use these features, you must create a class that implements them.

Practical Learning Practical Learning: Starting a Custom Collection Class

  1. Start Microsoft Visual C++ and create a new CLR Empty Project named SolasPropertyRental1
  2. To create a new class, in the Solution Explorer, right-click Solas Property Rental1 -> Add -> Class...
  3. In the Templates list, click C++ Class and click Add
  4. Set the Name to CRentalProperty and press Enter
  5. Change the header file as follows:
      
    #pragma once
    
    using namespace System;
    
    public enum class PropertyTypes
    {
    	Apartment = 1,
    	Townhouse,
    	SingleFamily,
    	Unknown
    };
    
    public enum class PropertyConditions
    {
    	Excellent = 1,
    	Good,
    	BadShape,
    	Unknown
    };
    
    public ref class CRentalProperty
    {
    private:
        // These are the characteristics of a property
        long propCode;
        PropertyTypes type;
        PropertyConditions cond;
        short beds;
        double baths;
        double val;
    
    public:
        property long PropertyCode
        {
            long get() { return propCode; }
            void set(long value) { propCode = value; }
        }
    
        property PropertyTypes PropertyType
        {
            PropertyTypes get() { return type; }
            void set(PropertyTypes value) { type = value; }
        }
    
        property PropertyConditions PropertyCondition
        {
            PropertyConditions get() { return cond; }
            void set(PropertyConditions value) { cond = value; }
        }
    
        property short Bedrooms
        {
            short get() { return beds; }
            void set(short value) { beds = value; }
        }
    
        property double Bathrooms
        {
            double get() { return (baths <= 0) ? 0.00 : baths; }
            void set(double value) { baths = value; }
        }
    
        property double MonthlyRent
        {
            double get() { return (val <= 0) ? 0.00 : val; }
            void set(double value) { val = value; }
        }
    
    public:
        // This constructor is used to create 
        // default values for a property
        CRentalProperty(void);
    };
  6. Open the RentalProperty.cpp source file and change it as follows:
       
    #include "RentalProperty.h"
    
    CRentalProperty::CRentalProperty(void)
    {
        propCode = 0;
        type     = PropertyTypes::Unknown;
        cond     = PropertyConditions::Unknown;
        beds     = 0;
        baths    = 0.0;
        val      = 0.00;
    }
  7. To create a new file, in the Solution Explorer, right-click Solas Property Rental1 -> Add -> New Item...
  8. In the Templates list, click C++ File (.cpp)
  9. Set the Name to Exercise and press Enter
  10. Change the file as follows:
      
    using namespace System;
    
    int main()
    {
        return 0;
    }
  11. Save all

Implementing IList

As mentioned above, to create a collection, you can derive it from the IList interface. Here is an example:

using namespace System::Collections;

public ref class CStudents : public IList
{
};

This IList interface is declared as follows:

public interface class IList : ICollection, 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:

public ref class CObjects : public IList
{
private:
    int items;
    array<Object ^> ^ objects;

public:
    CObjects();

    virtual property int Count
    {
        int get() { return items; }
    }

    virtual property bool IsSynchronized
    {
	bool get() { return false; }
    }

    virtual property Object ^ SyncRoot
    {
	Object ^ get() { return this; }
    }

    virtual void CopyTo(Array ^, int);
};

CObjects::CObjects()
{
    items = 0;
    objects = gcnew array<Object ^>(5);
}

void CObjects::CopyTo(Array ^ ary, int index)
{
    
}

You must also implement the GetEnumerator() method of the IEnumerable interface. If you don't have time to completely implement it, you can simply return nullptr. Here is an example:

using namespace System;
using namespace System::Collections;

public ref class CObjects : public IList
{
    . . . No Change

public:
    . . . No Change

    virtual void CopyTo(Array ^, int);
    virtual IEnumerator ^ GetEnumerator(void);
};

CObjects::CObjects()
{
    items = 0;
    objects = gcnew array<Object ^>(5);
}

IEnumerator ^ CObjects::GetEnumerator(void)
{
    return nullptr;
}

Practical Learning Practical Learning: Implementing the IList Interface

  1. To create a new class, in the Class View, right-click Solas Property Rental1 -> Add -> Class...
  2. In the Templates list, click C++ Class and click Add
  3. Set the Name to CProperties and press Enter
  4. Change the Properties.h header file as follows:
      
    #pragma once
    
    #include "RentalProperty.h"
    
    using namespace System;
    using namespace System::Collections;
    
    public ref class CProperties : public IList
    {
    private:
        array<Object ^> ^ RentalProperties;
        int counter;
    	
    public:
        CProperties(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 ^, int);
        virtual IEnumerator ^ GetEnumerator(void);
    };
  5. Open the RentalProperty.cpp source file and change it as follows:
       
    #include "Properties.h"
    
    CProperties::CProperties(void)
    {
        counter = 0;
        RentalProperties = gcnew array<Object ^>(5);
    }
    
    void CProperties::CopyTo(Array ^, int)
    {
    }
    
    IEnumerator ^ CProperties::GetEnumerator(void)
    {
        return nullptr;
    }
  6. Save all
 

Home Copyright © 2007-2012 FunctionX Next