|
1 |
| -// <Snippet6> |
2 |
| -using System; |
| 1 | +using System; |
3 | 2 |
|
4 |
| -public class Example |
| 3 | +namespace Chain1 |
5 | 4 | {
|
6 |
| - public static void Main() |
7 |
| - { |
8 |
| - var pages = new Pages(); |
9 |
| - if (!String.IsNullOrEmpty(pages.CurrentPage.Title)) { |
10 |
| - String title = pages.CurrentPage.Title; |
11 |
| - Console.WriteLine("Current title: '{0}'", title); |
12 |
| - } |
13 |
| - } |
14 |
| -} |
| 5 | + // <Snippet6> |
| 6 | + public class Chain1Example |
| 7 | + { |
| 8 | + public static void Main() |
| 9 | + { |
| 10 | + var pages = new Pages(); |
| 11 | + if (!string.IsNullOrEmpty(pages.CurrentPage.Title)) |
| 12 | + { |
| 13 | + string title = pages.CurrentPage.Title; |
| 14 | + Console.WriteLine("Current title: '{0}'", title); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
15 | 18 |
|
16 |
| -public class Pages |
17 |
| -{ |
18 |
| - Page[] page = new Page[10]; |
19 |
| - int ctr = 0; |
| 19 | + public class Pages |
| 20 | + { |
| 21 | + readonly Page[] _page = new Page[10]; |
| 22 | + int _ctr = 0; |
20 | 23 |
|
21 |
| - public Page CurrentPage |
22 |
| - { |
23 |
| - get { return page[ctr]; } |
24 |
| - set { |
25 |
| - // Move all the page objects down to accommodate the new one. |
26 |
| - if (ctr > page.GetUpperBound(0)) { |
27 |
| - for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++) |
28 |
| - page[ndx - 1] = page[ndx]; |
29 |
| - } |
30 |
| - page[ctr] = value; |
31 |
| - if (ctr < page.GetUpperBound(0)) |
32 |
| - ctr++; |
33 |
| - } |
34 |
| - } |
| 24 | + public Page CurrentPage |
| 25 | + { |
| 26 | + get { return _page[_ctr]; } |
| 27 | + set |
| 28 | + { |
| 29 | + // Move all the page objects down to accommodate the new one. |
| 30 | + if (_ctr > _page.GetUpperBound(0)) |
| 31 | + { |
| 32 | + for (int ndx = 1; ndx <= _page.GetUpperBound(0); ndx++) |
| 33 | + _page[ndx - 1] = _page[ndx]; |
| 34 | + } |
| 35 | + _page[_ctr] = value; |
| 36 | + if (_ctr < _page.GetUpperBound(0)) |
| 37 | + _ctr++; |
| 38 | + } |
| 39 | + } |
35 | 40 |
|
36 |
| - public Page PreviousPage |
37 |
| - { |
38 |
| - get { |
39 |
| - if (ctr == 0) { |
40 |
| - if (page[0] == null) |
41 |
| - return null; |
42 |
| - else |
43 |
| - return page[0]; |
44 |
| - } |
45 |
| - else { |
46 |
| - ctr--; |
47 |
| - return page[ctr + 1]; |
48 |
| - } |
49 |
| - } |
50 |
| - } |
51 |
| -} |
| 41 | + public Page PreviousPage |
| 42 | + { |
| 43 | + get |
| 44 | + { |
| 45 | + if (_ctr == 0) |
| 46 | + { |
| 47 | + if (_page[0] == null) |
| 48 | + return null; |
| 49 | + else |
| 50 | + return _page[0]; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + _ctr--; |
| 55 | + return _page[_ctr + 1]; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
52 | 60 |
|
53 |
| -public class Page |
54 |
| -{ |
55 |
| - public Uri URL; |
56 |
| - public String Title; |
| 61 | + public class Page |
| 62 | + { |
| 63 | + public Uri URL; |
| 64 | + public string Title; |
| 65 | + } |
| 66 | + // The example displays the following output: |
| 67 | + // Unhandled Exception: |
| 68 | + // System.NullReferenceException: Object reference not set to an instance of an object. |
| 69 | + // at Example.Main() |
| 70 | + // </Snippet6> |
57 | 71 | }
|
58 |
| -// The example displays the following output: |
59 |
| -// Unhandled Exception: |
60 |
| -// System.NullReferenceException: Object reference not set to an instance of an object. |
61 |
| -// at Example.Main() |
62 |
| -// </Snippet6> |
|
0 commit comments