Home

C++/CLI Collections:
The Size of a Collection

 

A Fixed-Size Collection

In the next sections, we will see how to add items to a collection. As you add or insert items in the list, the Count property grows. If your collection is array-based, when you start it, you specify the number of items that the list will contain. In theory, you cannot add new items beyond that number. In reality, you can increase the size of an array and then add a new item. If your collection is a linked list, you are also not confined to the laws of space (unless your computer runs out of memory.

If you create a list whose number of items must be constant, the user cannot add items beyond the maximum allowed number. Therefore, before adding an item, you can first check whether the collection has a fixed size or not. To give you this information, the IList interface is equipped with a Boolean read-only property named IsFxedSize. This property simply lets the user know whether the collection has a fixed number of items.

Here is an example of implementing this property:

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

public:
    . . . No Change

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

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

Practical Learning Practical Learning: Implementing the IsFixedSize Property

  • Access the Properties.h header file and add the following property:
     
    #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:
        . . . No Change
    
        virtual property bool IsFixedSize
        {
    	bool get() { return false; }
        }
    
        virtual void CopyTo(Array ^, int);
        virtual IEnumerator ^ GetEnumerator(void);
    };

A Read-Only Collection

Most collections are meant to receive new items. If you want, you can create a collection that cannot receive new values. To support this, the IList interface is equipped with the Boolean IsReadOnly property. If a collection is read-only, it would prevent the clients from adding items to it.

Here is an example of implementing the IsReadOnly property:

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

public:
    . . . No Change

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

    . . . No Change
};

Practical Learning Practical Learning: Implementing the IsReadOnly Property

  1. In the Properties.h header file, add the following property:
     
    #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:
        . . . No Change
    
        virtual property bool IsReadOnly
        {
    	bool get() { return false; }
        }
    
        virtual void CopyTo(Array ^, int);
        virtual IEnumerator ^ GetEnumerator(void);
    };
  2. In the Class View, right-click CProperties -> Add -> Function...
  3. Accept the Return Type as int and set the Function Name to Add
  4. In the Parameter Type, type Object ^
  5. In the Parameter Name, type value and click Add
  6. Click the virtual check box
     
  7. Click Finish
  8. In the Class View, right-click CProperties -> Add -> Function...
  9. Set the Return Type to void
  10. Set the Function Name to insert
  11. In the Parameter Type, select int
  12. In the Parameter Name, type index and click Add
  13. In the Parameter Type, type Object ^
  14. In the Parameter Name, type value and click Add
  15. Click the virtual check box
     
  16. Click Finish
  17. Open the Properties.h header file and add the following property
     
    #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:
        . . . No Change
    
        virtual property Object ^ default[int]
        {
    	Object ^ get(int index) { return nullptr; }
    	void set(int index, Object ^ value) { }
        }
    
        virtual void CopyTo(Array ^, int);
        virtual IEnumerator ^ GetEnumerator(void);
    
        . . . No Change
    };
  18. In the Class View, right-click CProperties -> Add -> Function...
  19. Set the Return Type to bool
  20. Set the Function Name to Contains
  21. In the Parameter Type, type Object ^
  22. In the Parameter Name, type value and click Add
  23. Click the virtual check box
     
  24. Click Finish
  25. In the Class View, right-click CProperties -> Add -> Function...
  26. Accept the Return Type as int and set the Function Name to IndexOf
  27. In the Parameter Type, type Object ^
  28. In the Parameter Name, type value and click Add
  29. Click the virtual check box
     
  30. Click Finish
  31. In the Class View, right-click CProperties -> Add -> Function...
  32. Set the Return Type to void
  33. Set the Function Name to RemoveAt
  34. In the Parameter Type, select int
  35. In the Parameter Name, type index and click Add
  36. Click the virtual check box
  37. Click Finish
  38. In the Class View, right-click CProperties -> Add -> Function...
  39. Set the Return Type to void
  40. Set the Function Name to Remove
  41. In the Parameter Type, select Object ^
  42. In the Parameter Name, type value and click Add
  43. Click the virtual check box
  44. Click Finish
  45. In the Class View, right-click CProperties -> Add -> Function...
  46. Set the Return Type to void
  47. Set the Function Name to Clear
  48. Click the virtual check box
  49. Click Finish
     
    #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 property bool IsFixedSize
        {
    	bool get() { return false; }
        }
    
        virtual property bool IsReadOnly
        {
    	bool get() { return false; }
        }
    
        virtual property Object ^ default[int]
        {
    	Object ^ get(int index) { return nullptr; }
    	void set(int index, Object ^ value) { }
        }
    
        virtual void CopyTo(Array ^, int);
        virtual IEnumerator ^ GetEnumerator(void);
    
        // This method is used to add a new item to the collection
        virtual int Add(Object ^ value);
        // This method can be used to insert an item at 
        // a certain position inside the collection
        virtual void Insert(int index, Object ^ value);
        // This method is used to find out whether the item 
        //  passed as argument exists in the collection
        virtual bool Contains(Object ^ value);
        // This method is used to check whether the item passed as
        // argument exists in the collection. If so, it returns its index
        virtual int IndexOf(Object ^ value);
        // This method is used to delete the item positioned 
        // at the index passed as argument
        virtual void RemoveAt(int index);
        // This method first checks the existence of the item passed 
        //  as argument. If the item exists, the method deletes it
        virtual void Remove(Object ^ value);
        // This methods deletes all items from the collection
        virtual void Clear(void);
    };
 

Previous Copyright © 2007-2012 FunctionX Next