Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

Let’s add these members to our implementation. Internally, this will also require a private integer to track the current position. This private member is not part of our exported implementation, so it was not shown in the previous table.

IMPLEMENT ALBUM POSITION OPERATIONS

 

Action

Result

 

 

 

1

In the PhotoAlbum.cs file,

/// <summary>

 

add a private integer

/// Tracks the current index position

 

_currentPos.

/// when displaying the album.

 

/// </summary>

 

 

 

 

private int _currentPos = 0;

 

 

 

2

Add the CurrentPosition

public int CurrentPosition

 

property to get or set this

{

 

position.

get { return _currentPos; }

 

 

 

How-to

set

 

For the get access

{

 

if (value <= 0)

 

method, simply return the

 

{

 

current value.

_currentPos = 0;

 

For the set access

}

 

else if (value >= this.Count)

 

method, make sure the

{

 

given value is in range.

_currentPos = this.Count - 1;

 

 

}

 

 

else

 

 

{

 

 

_currentPos = value;

 

 

}

 

 

}

 

 

}

 

 

 

3

Ensure that this value is

protected override void OnClear()

 

reset when the album is

{

 

cleared.

_currentPos = 0;

 

base.OnClear();

 

 

 

How-to

}

 

Override the OnClear

Note: The base keyword used here is provided by C# as

 

method.

a convenient way to reference the base class of the cur-

 

Note: This protected

rent object.

 

 

 

method is provided by the

 

 

CollectionBase class to

 

 

permit collection-specific

 

 

code to be executed

 

 

before the Clear method

 

 

is invoked.

 

 

 

 

4

Implement the

public Photograph CurrentPhoto

 

CurrentPhoto property.

{

 

 

get

 

 

{

 

 

if (this.Count == 0)

 

 

return null;

 

 

return this[CurrentPosition];

 

 

}

 

 

}

 

 

 

INTERFACES REVISITED

149

Pho-

IMPLEMENT ALBUM POSITION OPERATIONS (continued)

 

Action

Result

 

 

 

5

Implement the

public bool CurrentNext()

 

CurrentNext method.

{

 

How-to

if (CurrentPosition+1 < this.Count)

 

{

 

Use the CurrentPosition

CurrentPosition ++;

 

property to set and get the

return true;

 

}

 

current index.

 

 

 

 

return false;

 

 

}

 

 

 

6

Implement the

public bool CurrentPrev()

 

CurrentPrev method.

{

 

 

if (CurrentPosition > 0)

 

 

{

 

 

CurrentPosition --;

 

 

return true;

 

 

}

 

 

return false;

 

 

}

 

 

 

We can now add photographs to and remove photographs from our album, and track the current position for display purposes. Since we expect the CurrentPosition property to return a valid index, we should also update this setting whenever a tograph is removed from the album.

ENSURE ALBUM POSITION REMAINS VALID

 

Action

Result

 

 

 

7

In the PhotoAlbum.cs file,

protected override void OnRemoveComplete

 

override the

(int index, object val)

 

OnRemoveComplete

{

 

CurrentPosition = _currentPos;

 

method.

 

base.OnRemoveComplete(index, val);

 

Note: This protected

}

 

 

 

method is called after an

 

 

object is removed from

 

 

the contained collection.

 

 

 

 

As you can see, this code ensures that the current position is updated whenever an object is removed from the collection. This includes both the Remove and RemoveAt methods. By resetting the property, we ensure that the _currentPos variable is reset as appropriate for the new bounds of the album.

With the interfaces for our PhotoAlbum class fully implemented, let’s head back to the Photograph class to deal with various issues related to the robustness of our new library.

150

CHAPTER 5 REUSABLE LIBRARIES

Соседние файлы в папке c#