Home

C++/CLI .NET Support for Collections

 

Introduction to System Collections

In the previous lesson, we saw that, when creating a collection, you should provide a method that allows you to retrieve a member of the collection. You can list the members of an array or a collection through a technique called an enumeration. Enumerating a collection consists of visiting each member of the list, for any reason judged necessary. For example, you can enumerate a collection to display a list of its members. You can enumerate a collection when looking for a member that responds to a certain criterion.

Besides, or instead of, a for loop, the .NET Framework provides another and better support for enumeration. In the C++/CLI language, you can enumerate a collection using the for each operator, but the collection must be prepared for it: you cannot just use for each for any collection. This support is provided through two main interfaces: public IEnumerator and IEnumerable. These two interfaces are defined in the System::Collections namespace. Therefore, if you intend to use them, you can include this namespace in your source file.

Practical Learning Practical Learning: Introducing Built-In Collections

  1. Start Microsoft Visual C++ and create a new CLR Empty Project named BethesdaCarRental1
  2. To create a new class, in the Solution Explorer, right-click BethesdaCarRental1 -> Add -> Class...
  3. In the Templates list, click C++ Class and click Add
  4. Set the Name to CCar and press Enter
  5. Change the Car.h header file as follows:
     
    #pragma once
    
    using namespace System;
    
    public ref class CCar
    {
    public:
        String ^ TagNumber;
        String ^ Make;
        String ^ Model;
        short    CarYear;
        int      Mileage;
        String ^ Category;
        bool     HasK7Player;
        bool     HasCDPlayer;
        bool     HasDVDPlayer;
        bool     Available;
    
        CCar ^ Next;
    
        CCar(void);
    };
  6. Access the Car.cpp source file and implement the constructor as follows:
     
    #include "Car.h"
    
    CCar::CCar()
    {
        TagNumber    = L"000 000";
        Make         = L"Unknown";
        Model        = L"Unknown";
        CarYear      = 1960;
        Mileage      = 0;
        Category     = L"Unknown";
        HasK7Player  = false;
        HasCDPlayer  = false;
        HasDVDPlayer = false;
        Available    = false;
    }
  7. To create a new class, in the Solution Explorer, right-click BethesdaCarRental1 -> Add -> Class...
  8. In the Templates list, click C++ Class and click Add
  9. Set the Name to CInventory and click Finish
  10. Change the Inventory.h header file as follows:
     
    #pragma once
    
    #include "Car.h"
    
    using namespace System;
    
    public ref class CInventory abstract
    {
    protected:
        int items;
    
    public:
        CInventory(void);
       
        property int Count
        {
            int get() { return items; }
        }
    
        virtual int Add(CCar ^ obj) = 0;
        virtual CCar ^ Get(int index) = 0;
        virtual bool Delete() = 0;
    };
  11. Access the Inventory.cpp source file and change it as follows:
     
    #include "Inventory.h"
    
    CInventory::CInventory(void)
    {
        items = 0;
    }
  12. To create a new class, in the Class View, right-click BethesdaCarRental1 -> Add -> Class...
  13. In the Templates list, click C++ Class and click Add
  14. Set the Name to CCarInventory and click Finish
  15. Change the CarInventory.h header file as follows:
     
    #pragma once
    
    #include "Inventory.h"
    #include "Car.h"
    
    using namespace System;
    
    public ref class CCarInventory : public CInventory
    {
    public:
        CCar ^ Head;
        CCar ^ Current;
        CCar ^ Inventory;
    
        virtual int Add(CCar ^ NewCar) override;
        virtual CCar ^ Get(int index) override;
        virtual bool Delete() override;
    
        CCarInventory(void);
    };
  16. Access the CarInventory.cpp source file and change it as follows:
     
    #include "CarInventory.h"
    
    CCarInventory::CCarInventory(void)
    {
        Head = nullptr;
    }
    
    int CCarInventory::Add(CCar ^ NewCar)
    {
        CCar ^ Sample = gcnew CCar;
    
        Sample = NewCar;
        Sample->Next = Head;
        Head = Sample;
        return items++;
    }
    
    CCar ^ CCarInventory::Get(int index)
    {
        CCar ^ Current = Head;
    
        for(int i = Count - 1;
            i > index && Current != nullptr;
            i--)
            Current = Current->Next;
        return Current;
    }
    
    bool CCarInventory::Delete()
    {
        if (Head == nullptr)
        {
            Console::WriteLine(L"The inventory is empty");
            return false;
        }
    
        CCar ^ Current;
    
        Current = Head->Next;
        Head->Next = Current->Next;
        items--;
        return true;
    }
  17. Save all  
 

Home Copyright © 2007-2013, FunctionX Next