Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

36.4.3Коллекция только для чтения

Большинство коллекций предназначены для получения новых элементов. Если вы хотите, вы можете создать коллекцию, которая не сможет получать новые элементы. Для поддержки такой коллекции интерфейс IList оснащен свойством IsReadOnly. Если коллекция только для чтения, это будет препятствовать добавлению в нее элементов. Вот пример реализации IsReadOnly:

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

};

36.4.4Упражнение

  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);

};

  1. In the Class View, right-click CProperties -> Add -> Function...

  2. Accept the Return Type as int and set the Function Name to Add

  3. In the Parameter Type, type Object ^

  4. In the Parameter Name, type value and click Add

  5. Click the virtual check box  

  6. Click Finish

  7. In the Class View, right-click CProperties -> Add -> Function...

  8. Set the Return Type to void

  9. Set the Function Name to insert

  10. In the Parameter Type, select int

  11. In the Parameter Name, type index and click Add

  12. In the Parameter Type, type Object ^

  13. In the Parameter Name, type value and click Add

  14. Click the virtual check box  

  15. Click Finish

  16. 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

};

  1. In the Class View, right-click CProperties -> Add -> Function...

  2. Set the Return Type to bool

  3. Set the Function Name to Contains

  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. Accept the Return Type as int and set the Function Name to IndexOf

  10. In the Parameter Type, type Object ^

  11. In the Parameter Name, type value and click Add

  12. Click the virtual check box  

  13. Click Finish

  14. In the Class View, right-click CProperties -> Add -> Function...

  15. Set the Return Type to void

  16. Set the Function Name to RemoveAt

  17. In the Parameter Type, select int

  18. In the Parameter Name, type index and click Add

  19. Click the virtual check box

  20. Click Finish

  21. In the Class View, right-click CProperties -> Add -> Function...

  22. Set the Return Type to void

  23. Set the Function Name to Remove

  24. In the Parameter Type, select Object ^

  25. In the Parameter Name, type value and click Add

  26. Click the virtual check box

  27. Click Finish

  28. In the Class View, right-click CProperties -> Add -> Function...

  29. Set the Return Type to void

  30. Set the Function Name to Clear

  31. Click the virtual check box

  32. 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