The code herein demonstrates find, findall and replace predicate usage.
The form's code-behind file is printed below, but you may download the entire 2008 solution:
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Windows.Forms;
5
6 namespace PredicatesVsForLoops
7 {
8 public partial class frmListMethodUsage : Form
9 {
10 #region Constructors
11 /// <summary>
12 /// Constructor: starts up the form object
13 /// </summary>
14 public frmListMethodUsage()
15 {
16 InitializeComponent();
17 _formatTxtBox();
18 }
19 #endregion
20
21 #region InternalMembers
22 private double _findTimeForSingleElement;
23 private double _findTimeForMultiElements;
24 private double _findTimeForSingleElementWithForLoop;
25 private double _findTimeForMultiElementsWithForLoop;
26 private double _removeTime;
27 private double _removeTimeWithForLoop;
28
29 private int _arrayMemberIdx;
30 #endregion
31
32 #region StringConstants
33 private const string _valueProperty = "Value";
34 //private const string _nameProperty = "Name";
35 private const string _commonValue = "Same";
36 //private const string _nonExistentValue = "NonExistentValue";
37 private const string _setIntro = " Set # ";
38 private const string _fooObjectName = "foo";
39 private const string _msgNoResults = "no results found";
40 //private const string _msgResultsFoundIntro = "The items found";
41 private const string _msgTimeElapsed = "Time elapsed for";
42 private const string _findFooMethod = "_findFoo() method";
43 private const string _findFooInForLoopMethod = "_findFooInForLoop() method";
44 private const string _findFoosMethod = "_findFoos() method";
45 private const string _findFoosInForLoopMethod = "_findFoosInForLoop() method";
46 private const string _removeFooMethod = "_removeFoo() method";
47 private const string _removeFooWithForLoopMethod = "_removeFooWithLoop() method";
48 private const string _seconds = "seconds";
49 private const string _nameFound = "Name of item found";
50 private const string _valueFound = "Value of item found";
51 private const string _qtyFound = "Number of items found";
52 private const string _qtyRemoved = "Number of items removed";
53 private const string _firstFound = "First Item Found";
54 private const string _lastFound = "Last Item Found";
55 private const string _spacerLine =
56 "--------------------------------------------------------";
57 private const string _no = "There was no";
58 private const string _winningMethod = "Winning Method";
59 private const string _timeDiff = "Time Difference";
60 private const string _arrayCreation = "Array creation";
61 private const string _listCreation = "List creation";
62 private const string _durationWas = "Duration was";
63 private const string _for = "for";
64 private const string _items = "items";
65 private const string _listAddFailedAt = "List.Add method failed at item #";
66 private const string _arrayAddFailedAt = "Array member access failed at item #";
67 private const string _success = "SUCCESS";
68 private const string _failure = "FAILURE";
69 private const string _outOfMemoryCustomErr =
70 "Out of memory error: view details in text box";
71 private const string _otherCustomErr =
72 "Other error: view details in text box";
73 #endregion
74
75 #region Events
76 private void btnRunAll_Click(object sender, EventArgs e)
77 {
78 Cursor curr = Cursor;
79 Cursor = Cursors.WaitCursor;
80
81 btnClear_Click(null, null);
82 btnDemoFind_Click(null, null);
83 _addToTxtBox(Environment.NewLine + _spacerLine, Color.Black, true,
84 false, false, 14);
85 btnDemoForLoopFind_Click(null, null);
86 _showWinner(_findFooMethod, _findTimeForSingleElement,
87 _findFooInForLoopMethod, _findTimeForSingleElementWithForLoop);
88
89 btnDemoFindAll_Click(null, null);
90 _addToTxtBox(Environment.NewLine + _spacerLine, Color.Black, true,
91 false, false, 14);
92 btnDemoForLoopFindAll_Click(null, null);
93 _showWinner(_findFoosMethod, _findTimeForMultiElements,
94 _findFoosInForLoopMethod, _findTimeForMultiElementsWithForLoop);
95
96 btnDemoRemove_Click(null, null);
97 _addToTxtBox(Environment.NewLine + _spacerLine, Color.Black, true,
98 false, false, 14);
99 btnDemoForLoopRemove_Click(null, null);
100 _showWinner(_removeFooMethod, _removeTime,
101 _removeFooWithForLoopMethod, _removeTimeWithForLoop);
102
103 Cursor = curr;
104 }
105
106 /// <summary>
107 /// Button click event that shows implementation
108 /// of the Find method for a List of objects
109 /// </summary>
110 /// <param name="sender">Object that requested the event</param>
111 /// <param name="e">Event arguments</param>
112 private void btnDemoFind_Click(object sender, EventArgs e)
113 {
114 Cursor curr = Cursor;
115 Cursor = Cursors.WaitCursor;
116
117 long start = DateTime.Now.Ticks;
118 int qty = _getQty(txtItemQty);
119
120 List<Foo> findList = _getFooListWithDistinctValues(qty);
121 Foo f = _findFoo(findList, _valueProperty + (qty - 2));
122
123 //if we run this line INSTEAD of prior, we will receive NO results-here for testing/demo
124 //Foo m = _findFoo(findList, _valueProperty + 1000000);
125
126 double ms = _getDuration(start);
127 _findTimeForSingleElement = ms;
128 _showResultsOfFindMethod(f, ms, _findFooMethod);
129
130 Cursor = curr;
131 }
132
133 /// <summary>
134 /// Button click event that shows implementation
135 /// of the FindAll method for a List of objects
136 /// </summary>
137 /// <param name="sender">Object that requested the event</param>
138 /// <param name="e">Event arguments</param>
139 private void btnDemoFindAll_Click(object sender, EventArgs e)
140 {
141 Cursor curr = Cursor;
142 Cursor = Cursors.WaitCursor;
143
144 long start = DateTime.Now.Ticks;
145 int qty = _getQty(txtItemQty);
146
147 List<Foo> findList = _getFooListWithMatchingValues(qty);
148 List<Foo> foundList = _findFoos(findList, _commonValue + _valueProperty);
149
150 //if we run this line INSTEAD of prior, we will receive NO results-here for testing/demo
151 //List<Foo> list = _findFoos(findList, _nonExistentValue);
152
153 double ms = _getDuration(start);
154 _findTimeForMultiElements = ms;
155 _showResultsOfFindAllMethod(foundList, ms, _findFoosMethod);
156
157 Cursor = curr;
158 }
159
160 private void btnDemoRemove_Click(object sender, EventArgs e)
161 {
162 Cursor curr = Cursor;
163 Cursor = Cursors.WaitCursor;
164
165 long start = DateTime.Now.Ticks;
166 int qty = _getQty(txtItemQty);
167
168 List<Foo> fullList = _getFooListWithSomeSharedValues(qty);
169 List<Foo> filteredList = _removeFoo(fullList, _valueProperty + _setIntro + 8);
170
171 //if we run this line INSTEAD of prior, we will receive same results as fullList-here for testing/demo
172 //List<Foo> filteredList = _removeFoo(findList, _nonExistentValue);
173
174 double ms = _getDuration(start);
175 _removeTime = ms;
176
177 _showResultsOfRemoveMethod(fullList, filteredList, ms, _removeFooMethod);
178
179 Cursor = curr;
180 }
181
182 private void btnDemoForLoopRemove_Click(object sender, EventArgs e)
183 {
184 Cursor curr = Cursor;
185 Cursor = Cursors.WaitCursor;
186
187 long start = DateTime.Now.Ticks;
188 int qty = _getQty(txtItemQty);
189
190 Foo[] fullArray = _getFooArrayWithSomeSharedValues(qty);
191 Foo[] filteredArray = _removeFooWithLoop(fullArray, _valueProperty + _setIntro + 8);
192
193 //if we run this line INSTEAD of prior, we will receive same results as fullList-here for testing/demo
194 //List<Foo> filteredList = _removeFoo(findList, _nonExistentValue);
195
196 double ms = _getDuration(start);
197 _removeTimeWithForLoop = ms;
198
199 _showResultsOfRemoveMethod(fullArray, filteredArray, ms, _removeFooWithForLoopMethod);
200
201 Cursor = curr;
202 }
203
204 /// <summary>
205 /// Button click event that shows implementation
206 /// of the old standard for...loop to find an item in a List of objects
207 /// </summary>
208 /// <param name="sender">Object that requested the event</param>
209 /// <param name="e">Event arguments</param>
210 private void btnDemoForLoopFind_Click(object sender, EventArgs e)
211 {
212 Cursor curr = Cursor;
213 Cursor = Cursors.WaitCursor;
214
215 long start = DateTime.Now.Ticks;
216 int qty = _getQty(txtItemQty);
217
218 List<Foo> findList = _getFooListWithDistinctValues(qty);
219 Foo f = _findFooInForLoop(findList, _valueProperty + (qty - 2));
220
221 //if we run this line INSTEAD of prior, we will receive NO results-here for testing/demo
222 //Foo m = _findFoo(findList, _valueProperty + 1000000);
223
224 double ms = _getDuration(start);
225 _findTimeForSingleElementWithForLoop = ms;
226 _showResultsOfFindMethod(f, ms, _findFooInForLoopMethod);
227
228 Cursor = curr;
229 }
230
231 /// <summary>
232 /// Button click event that shows implementation
233 /// of the old standard for...loop to find all items in a List of objects
234 /// matching specified value
235 /// </summary>
236 /// <param name="sender">Object that requested the event</param>
237 /// <param name="e">Event arguments</param>
238 private void btnDemoForLoopFindAll_Click(object sender, EventArgs e)
239 {
240 Cursor curr = Cursor;
241 Cursor = Cursors.WaitCursor;
242
243 long start = DateTime.Now.Ticks;
244 int qty = _getQty(txtItemQty);
245
246 List<Foo> findList = _getFooListWithMatchingValues(qty);
247 List<Foo> foundList = _findFoosInForLoop(findList, _commonValue + _valueProperty);
248
249 //if we run this line INSTEAD of prior, we will receive NO results-here for testing/demo
250 //List<Foo> list = _findFoos(findList, _nonExistentValue);
251
252 double ms = _getDuration(start);
253 _findTimeForMultiElementsWithForLoop = ms;
254 _showResultsOfFindAllMethod(foundList, ms, _findFoosInForLoopMethod);
255
256 Cursor = curr;
257 }
258
259 /// <summary>
260 /// Resets the text box contents and formats
261 /// </summary>
262 /// <param name="sender">object that called this method</param>
263 /// <param name="e">Event arguments</param>
264 private void btnClear_Click(object sender, EventArgs e)
265 {
266 txtRpt.ResetBackColor();
267 txtRpt.ResetFont();
268 txtRpt.ResetText();
269 }
270
271 /// <summary>
272 /// Grabs quantity from text box and adds integers to a List object
273 /// </summary>
274 /// <param name="sender">calling object</param>
275 /// <param name="e">Event arguments</param>
276 private void btnTestListAddLimits_Click(object sender, EventArgs e)
277 {
278 Cursor curr = Cursor;
279 Cursor = Cursors.WaitCursor;
280
281 long start = DateTime.Now.Ticks;
282 int qty = _getQty(txtQtyXtra);
283
284 List<int> fooList = new List<int>();
285 int i = 0;
286 string msg = string.Empty;
287 try
288 {
289 for (i = 0; i < qty; i++)
290 fooList.Add(i);
291 }
292 catch (OutOfMemoryException oe)
293 {
294 msg = oe.Message + " - " + _listAddFailedAt + " " + i;
295 MessageBox.Show(_outOfMemoryCustomErr);
296 }
297 catch (Exception ex)
298 {
299 msg = ex.Message + " - " + _listAddFailedAt + " " + i;
300 MessageBox.Show(_otherCustomErr);
301 }
302 finally
303 {
304 double ms = _getDuration(start);
305 msg += Environment.NewLine + _durationWas + ": " + ms / 1000 +
306 " " + _seconds + " " + _for + " " + i + " " + _items;
307 _addToTxtBox(msg, Color.Black, false, false, false, 13);
308 }
309 Cursor = curr;
310 }
311
312 /// <summary>
313 /// Grabs quantity from text box, builds a List and an Array of Foo
314 /// for specified quantity, and reports upon success/failure, and exec time
315 /// </summary>
316 /// <param name="sender">calling object</param>
317 /// <param name="e">Event arguments</param>
318 private void btnCompareListAndArrayCreation_Click(object sender, EventArgs e)
319 {
320 Cursor curr = Cursor;
321 Cursor = Cursors.WaitCursor;
322
323 long start = DateTime.Now.Ticks;
324 int qty = _getQty(txtQtyXtra);
325
326 Foo[] fooArray = _getFooArrayWithDistinctValues(qty);
327 double ms = _getDuration(start);
328
329 if (fooArray == null)
330 _showResultsOfCreate(_arrayCreation, qty, 0, ms);
331 else
332 _showResultsOfCreate(_arrayCreation, qty, _arrayMemberIdx, ms);
333
334 start = DateTime.Now.Ticks;
335 List<Foo> fooList = _getFooListWithDistinctValues(qty);
336 ms = _getDuration(start);
337
338 if (fooList == null)
339 _showResultsOfCreate(_listCreation, qty, 0, ms);
340 else
341 _showResultsOfCreate(_listCreation, qty, fooList.Count, ms);
342
343 Cursor = curr;
344 }
345 #endregion
346
347 #region RichTextBoxMethods
348 /// <summary>
349 /// To be called after object initialization---sets some style props
350 /// </summary>
351 private void _formatTxtBox()
352 {
353 txtRpt.Font = new Font("Courier New", 11);
354 txtRpt.WordWrap = true;
355 txtRpt.ScrollBars = RichTextBoxScrollBars.Vertical;
356 }
357
358 /// <summary>
359 /// Appends any existing text in the text box, can be formatted
360 /// </summary>
361 /// <param name="txtToAdd">text to add to box</param>
362 /// /// <param name="color">Color to set font to</param>
363 /// <param name="bold">whether to bold this text</param>
364 /// <param name="italic">whether to italicize this text</param>
365 /// <param name="bullet">whether to bullet this text</param>
366 /// <param name="size">font size for this text</param>
367 private void _addToTxtBox(string txtToAdd, Color color, bool bold,
368 bool italic, bool bullet, int size)
369 {
370 if (string.IsNullOrEmpty(txtToAdd))
371 return;
372
373 int beg = txtRpt.TextLength;
374 txtRpt.AppendText(txtToAdd);
375 if (beg == 0)
376 _formatTxt(beg, txtToAdd.Length - 1, color, bold, italic, bullet, size);
377 else
378 _formatTxt(beg, txtToAdd.Length, color, bold, italic, bullet, size);
379 }
380
381 /// <summary>
382 /// Formats text in the text box, from start position to end position
383 /// as specified by arguments
384 /// </summary>
385 /// <param name="startPos">character number to begin formatting from</param>
386 /// <param name="endPos">character number to end formatting at</param>
387 /// <param name="color">Color to set font to</param>
388 /// <param name="bold">whether to bold this text</param>
389 /// <param name="italic">whether to italicize this text</param>
390 /// <param name="bullet">whether to bullet this text</param>
391 /// <param name="size">font size for this text</param>
392 private void _formatTxt(int startPos, int endPos, Color color, bool bold,
393 bool italic, bool bullet, int size)
394 {
395 if (txtRpt.TextLength < startPos + (endPos - startPos))
396 return;
397
398 txtRpt.Select(startPos, endPos);
399 txtRpt.SelectionColor = color;
400 FontStyle style = _getFontStyle(bold, italic);
401 txtRpt.SelectionFont = new Font(txtRpt.Font.FontFamily, size, style);
402 txtRpt.SelectionBullet = bullet;
403 }
404
405 /// <summary>
406 /// Evaluates bold & italic choices to get style enum
407 /// </summary>
408 /// <param name="bold">whether to bold the font</param>
409 /// <param name="italic">whether to italicize the font</param>
410 /// <returns>FontStyle object</returns>
411 private static FontStyle _getFontStyle(bool bold, bool italic)
412 {
413 if (bold && italic)
414 return FontStyle.Bold | FontStyle.Italic;
415 if (bold)
416 return FontStyle.Bold;
417 if (italic)
418 return FontStyle.Italic;
419 return FontStyle.Regular;
420 }
421 #endregion
422
423 #region ReportResultsMethods
424 /// <summary>
425 /// Show msg box if no results, otherwise, summarize duration + item found in text box
426 /// </summary>
427 /// <param name="f">Foo object</param>
428 /// <param name="milliseconds">double: number of milliseconds</param>
429 /// <param name="method">name of method being tested</param>
430 private void _showResultsOfFindMethod(Foo f, double milliseconds, string method)
431 {
432 if (f == null)
433 {
434 txtRpt.Text = _msgNoResults;
435 MessageBox.Show(_msgNoResults);
436 return;
437 }
438
439 Color nameColor = Color.DeepSkyBlue;
440 Color valueColor = Color.DarkCyan;
441 string data = Environment.NewLine + _msgTimeElapsed + " " +
442 method + ": ";
443 _addToTxtBox(data, nameColor, true, false, false, 13);
444
445 string ms = (milliseconds / 1000).ToString();
446 data = ms + " " + _seconds;
447 _addToTxtBox(data, valueColor, false, true, false, 12);
448
449 data = Environment.NewLine + _nameFound + ": ";
450 _addToTxtBox(data, nameColor, true, false, false, 13);
451
452 data = f.Name;
453 _addToTxtBox(data, valueColor, false, true, false, 12);
454
455 data = Environment.NewLine + _valueFound + ": ";
456 _addToTxtBox(data, nameColor, true, false, false, 13);
457
458 data = f.Value;
459 _addToTxtBox(data, valueColor, false, true, false, 12);
460 }
461
462 /// <summary>
463 /// Show msg box if no results, otherwise, summarize duration + 1st &
464 /// last item found in text box
465 /// </summary>
466 /// <param name="list">List of Foo objects</param>
467 /// <param name="milliseconds">double: number of milliseconds</param>
468 private void _showResultsOfFindAllMethod(IList<Foo> list, double milliseconds,
469 string method)
470 {
471 if (list == null || list.Count < 1)
472 {
473 txtRpt.Text = _msgNoResults;
474 MessageBox.Show(_msgNoResults);
475 return;
476 }
477
478 Color nameColor = Color.DarkGreen;
479 Color valColor = Color.ForestGreen;
480 string data = Environment.NewLine + _msgTimeElapsed + " " +
481 method + ": ";
482 _addToTxtBox(data, nameColor, true, false, false, 13);
483
484 string ms = (milliseconds / 1000).ToString();
485 data = ms + " " + _seconds;
486 _addToTxtBox(data, valColor, false, true, false, 12);
487
488 data = Environment.NewLine + _qtyFound + ": ";
489 _addToTxtBox(data, nameColor, true, false, false, 13);
490
491 data = list.Count.ToString();
492 _addToTxtBox(data, valColor, false, true, false, 12);
493
494 data = Environment.NewLine + _firstFound + ": ";
495 _addToTxtBox(data, nameColor, true, false, false, 13);
496
497 data = list[0].Name + " - " + list[0].Value;
498 _addToTxtBox(data, valColor, false, true, false, 12);
499
500 data = Environment.NewLine + _lastFound + ": ";
501 _addToTxtBox(data, nameColor, true, false, false, 13);
502
503 data = list[list.Count - 1].Name + " - " +
504 list[list.Count - 1].Value;
505 _addToTxtBox(data, valColor, false, true, false, 12);
506
507 //foreach (Foo item in list)
508 //...
509 }
510
511 /// <summary>
512 /// Show msg box if no results, otherwise, summarize duration + 1st &
513 /// last item found in text box
514 /// </summary>
515 /// <param name="origList">full list of foo objects/param>
516 /// <param name="newList">filtered list of foo objects/param>
517 /// <param name="milliseconds">double: number of milliseconds</param>
518 /// <param name="method">string name of method to report on</param>
519 private void _showResultsOfRemoveMethod(ICollection<Foo> origList,
520 ICollection<Foo> newList, double milliseconds, string method)
521 {
522 if (origList == null || origList.Count < 1)
523 {
524 txtRpt.Text = _msgNoResults;
525 MessageBox.Show(_msgNoResults);
526 return;
527 }
528
529 Color nameColor = Color.DarkGreen;
530 Color valColor = Color.ForestGreen;
531 string data = Environment.NewLine + _msgTimeElapsed + " " +
532 method + ": ";
533 _addToTxtBox(data, nameColor, true, false, false, 13);
534
535 string ms = (milliseconds / 1000).ToString();
536 data = ms + " " + _seconds;
537 _addToTxtBox(data, valColor, false, true, false, 12);
538
539 data = Environment.NewLine + _qtyRemoved + ": ";
540 _addToTxtBox(data, nameColor, true, false, false, 13);
541
542 int diff = origList.Count > newList.Count
543 ? origList.Count - newList.Count : 0;
544
545 data = diff.ToString();
546 _addToTxtBox(data, valColor, false, true, false, 12);
547 }
548
549 /// <summary>
550 /// Build text to report upon/display collection creation in txt ctrl
551 /// </summary>
552 /// <param name="_collectionIntro">
553 /// string to intro the collection TYPE (array/list..)
554 /// </param>
555 /// <param name="qty">collection qty requested</param>
556 /// <param name="count">actual collection count</param>
557 /// <param name="duration">milliseconds (time elapsed to make collection)</param>
558 private void _showResultsOfCreate(string _collectionIntro, int qty, int count,
559 double duration)
560 {
561 string txt;
562 if (count == 0 || count < qty)
563 txt = Environment.NewLine + _failure + ": ";
564 else
565 txt = Environment.NewLine + _success + ": ";
566
567 txt += _collectionIntro + " " + _durationWas + ": " +
568 duration / 1000 + " " + _seconds + " " + _for + " " +
569 count + " " + _items;
570
571 _addToTxtBox(txt, Color.Black, false, false, false, 12);
572 }
573 #endregion
574
575 #region MathMethods
576 /// <summary>
577 /// Takes start time, uses NOW time to compute time difference
578 /// and return double (difference in milliseconds) value
579 /// </summary>
580 /// <param name="start">
581 /// use DateTime.Now.Ticks to correspond with end comparison
582 /// </param>
583 /// <returns>Difference in milliseconds from start to now (end)</returns>
584 private static double _getDuration(long start)
585 {
586 long end = DateTime.Now.Ticks;
587 long elapsed = end - start;
588 return new TimeSpan(elapsed).TotalMilliseconds;
589 }
590
591 /// <summary>
592 /// Evaluates whether control text can be used for quantity
593 /// returns default quantity value, if not
594 /// </summary>
595 /// <param name="ctrl">Control that contains the text property</param>
596 /// <returns>int to use as quantity</returns>
597 private static int _getQty(Control ctrl)
598 {
599 long l;
600
601 return long.TryParse(ctrl.Text, out l)
602 ? Convert.ToInt32(ctrl.Text)
603 : 1000000;
604 }
605
606 /// <summary>
607 /// Computes difference between method times, and displays
608 /// winning method (if exists) and a dashed line in the text box
609 /// </summary>
610 /// <param name="method1Name">name of 1st method</param>
611 /// <param name="method1Time">duration of 1st method</param>
612 /// <param name="method2Name">name of 2nd method</param>
613 /// <param name="method2Time">duration of 2nd method</param>
614 private void _showWinner(string method1Name, double method1Time,
615 string method2Name, double method2Time)
616 {
617 string txt;
618 if (method1Time < method2Time)
619 txt = _winningMethod + ": " + method1Name + " - " + _timeDiff +
620 ": " + ((method2Time - method1Time) / 1000);
621 else if (method2Time < method1Time)
622 txt = _winningMethod + ": " + method2Name + " - " + _timeDiff +
623 ": " + ((method1Time - method2Time) / 1000);
624 else
625 txt = _no + " " + _winningMethod;
626
627 _addToTxtBox(Environment.NewLine + txt, Color.Purple, false, true, true, 12);
628 _addToTxtBox(Environment.NewLine + _spacerLine, Color.Black, true, false, false, 14);
629 }
630 #endregion
631
632 #region PredicateMethods
633 /// <summary>
634 /// Uses predicate delegate to search Foo list and return
635 /// the first foo whose value matches the specified fooValue
636 /// </summary>
637 /// <param name="findList">List of foo to search thru</param>
638 /// <param name="fooValue">Foo value property value to retrieve Foo object by</param>
639 /// <returns>Foo object</returns>
640 private static Foo _findFoo(List<Foo> findList, string fooValue)
641 {
642 Foo foo = findList.Find(delegate(Foo f)
643 { return f.Value.Equals(fooValue); });
644 return foo;
645 }
646
647 /// <summary>
648 /// Uses predicate delegate to search Foo list and return
649 /// all elements whose value match the specified fooValue
650 /// </summary>
651 /// <param name="findList">List of foo to search thru</param>
652 /// <param name="fooValue">Foo value property value to retrieve Foo objects by</param>
653 /// <returns>Foo object list</returns>
654 private static List<Foo> _findFoos(List<Foo> findList, string fooValue)
655 {
656 List<Foo> returnList = findList.FindAll(delegate(Foo f)
657 { return f.Value.Equals(fooValue); });
658 return returnList;
659 }
660
661 /// <summary>
662 /// Uses predicate delegate to search Foo list and remove
663 /// all elements whose value match the specified fooValue
664 /// </summary>
665 /// <param name="fullList">List of foo to search/filter</param>
666 /// <param name="fooValue">Foo value to remove Foo objects for</param>
667 /// <returns>Foo object list</returns>
668 private static List<Foo> _removeFoo(IEnumerable<Foo> fullList, string fooValue)
669 {
670 List<Foo> filteredList = new List<Foo>(fullList);
671 filteredList.RemoveAll(delegate(Foo f)
672 { return f.Value.Equals(fooValue); });
673 return filteredList;
674 }
675 #endregion
676
677 #region ForLoopMethods
678 /// <summary>
679 /// Uses for loop to iterate Foo list and return
680 /// the first foo whose value matches the specified fooValue
681 /// </summary>
682 /// <param name="findList">List of foo to loop thru</param>
683 /// <param name="fooValue">Foo value property value to retrieve Foo object by</param>
684 /// <returns>Foo object</returns>
685 private static Foo _findFooInForLoop(IEnumerable<Foo> findList, string fooValue)
686 {
687 foreach (Foo f in findList)
688 if (f.Value == fooValue)
689 return f;
690 return null;
691 }
692
693 /// <summary>
694 /// Uses for loop to iterate Foo list and return all
695 /// items whose value matches the specified fooValue
696 /// </summary>
697 /// <param name="findList">List of foo to loop thru</param>
698 /// <param name="fooValue">Foo value to retrieve Foo object by</param>
699 /// <returns>List of Foo objects</returns>
700 private static List<Foo> _findFoosInForLoop(IEnumerable<Foo> findList, string fooValue)
701 {
702 List<Foo> returnList = new List<Foo>();
703 foreach (Foo f in findList)
704 if (f.Value == fooValue)
705 returnList.Add(f);
706 return returnList;
707 }
708
709 /// <summary>
710 /// Uses for loop to iterate Foo array and remove all
711 /// items whose value matches the specified fooValue
712 /// </summary>
713 /// <param name="fullArray">Array of foo to loop thru</param>
714 /// <param name="fooValue">Foo value to remove Foo object by</param>
715 /// <returns>Filtered Array of Foo objects</returns>
716 private static Foo[] _removeFooWithLoop(ICollection<Foo> fullArray, string fooValue)
717 {
718 Foo[] filteredArray = new Foo[fullArray.Count];
719 int i = 0;
720 int subtract = 0;
721 foreach (Foo origFoo in fullArray)
722 {
723 if (origFoo.Value != fooValue)
724 filteredArray
= origFoo;
725 else
726 subtract += 1;
727 i++;
728 }
729 Foo[] finalArray = new Foo[i - subtract];
730
731 i = 0;
732 foreach (Foo newFoo in finalArray)
733 {
734 finalArray
= filteredArray
;
735 i++;
736 }
737 return finalArray;
738 }
739 #endregion
740
741 #region BuildListMethods
742 private static Foo[] _getFooArrayWithSomeSharedValues(int qty)
743 {
744 //force it:
745 if (qty < 10)
746 qty = 10;
747
748 Foo[] fooArray = new Foo[qty];
749 int i = 0;
750
751 try
752 {
753 for (int j = 0; j < 10; j++)
754 for (i = i; i < ((qty / 10) * (j + 1)); i++)
755 fooArray
= new Foo(_fooObjectName + i, _valueProperty +
756 _setIntro + (j + 1));
757 }
758 catch (OutOfMemoryException oe)
759 {
760 MessageBox.Show(oe.Message + " - " + _listAddFailedAt + " " + i);
761 return fooArray;
762 }
763 catch (Exception ex)
764 {
765 MessageBox.Show(ex.Message + " - " + _listAddFailedAt + " " + i);
766 return fooArray;
767 }
768 return fooArray;
769 }
770 /// <summary>
771 /// Creates and fills a foo list with some shared/some distinct values for each foo
772 /// </summary>
773 /// <param name="qty">number of foo to add to list</param>
774 /// <returns>foo list of qty specified w/various values</returns>
775 private static List<Foo> _getFooListWithSomeSharedValues(int qty)
776 {
777 //force it:
778 if (qty < 10)
779 qty = 10;
780
781 List<Foo> fooList = new List<Foo>();
782 int i = 0;
783
784 try
785 {
786 for (int j = 0; j < 10; j++)
787 for (i = i; i < ((qty / 10) * (j + 1)); i++)
788 fooList.Add(new Foo(_fooObjectName + i, _valueProperty +
789 _setIntro + (j + 1)));
790 }
791 catch (OutOfMemoryException oe)
792 {
793 MessageBox.Show(oe.Message + " - " + _listAddFailedAt + " " + i);
794 return fooList;
795 }
796 catch (Exception ex)
797 {
798 MessageBox.Show(ex.Message + " - " + _listAddFailedAt + " " + i);
799 return fooList;
800 }
801 return fooList;
802 }
803
804 /// <summary>
805 /// Creates and fills a foo list with distinct values for each foo
806 /// </summary>
807 /// <param name="qty">number of foo to add to list</param>
808 /// <returns>foo list of qty specified w/various values</returns>
809 private static List<Foo> _getFooListWithDistinctValues(int qty)
810 {
811 List<Foo> fooList = new List<Foo>();
812 int i = 0;
813 try
814 {
815 for (i = 0; i < qty; i++)
816 fooList.Add(new Foo(_fooObjectName + i, _valueProperty + i));
817 }
818 catch (OutOfMemoryException oe)
819 {
820 MessageBox.Show(oe.Message + " - " + _listAddFailedAt + " " + i);
821 return fooList;
822 }
823 catch (Exception ex)
824 {
825 MessageBox.Show(ex.Message + " - " + _listAddFailedAt + " " + i);
826 return fooList;
827 }
828 return fooList;
829 }
830
831 /// <summary>
832 /// Creates and fills a foo list with the SAME value for each foo
833 /// </summary>
834 /// <param name="qty">number of foo to add to list</param>
835 /// <returns>foo list of qty specified w/the same values</returns>
836 private static List<Foo> _getFooListWithMatchingValues(int qty)
837 {
838 List<Foo> fooList = new List<Foo>();
839 int i = 0;
840 try
841 {
842 for (i = 0; i < qty; i++)
843 fooList.Add(new Foo(_fooObjectName + i, _commonValue +
844 _valueProperty));
845 }
846 catch (OutOfMemoryException oe)
847 {
848 MessageBox.Show(oe.Message + " - " + _listAddFailedAt + " " + i);
849 return fooList;
850 }
851 catch (Exception ex)
852 {
853 MessageBox.Show(ex.Message + " - " + _listAddFailedAt + " " + i);
854 return fooList;
855 }
856 return fooList;
857 }
858
859 /// <summary>
860 /// Creates and fills a foo Array with distinct values for each foo
861 /// </summary>
862 /// <param name="qty">number of foo to add to array</param>
863 /// <returns>foo list of qty specified w/various values</returns>
864 private Foo[] _getFooArrayWithDistinctValues(int qty)
865 {
866 _arrayMemberIdx = 0;
867 Foo[] fooArray = new Foo[qty];
868 int i = 0;
869 try
870 {
871 for (i = 0; i < qty; i++)
872 fooArray
= new Foo(_fooObjectName + i, _valueProperty + i);
873 }
874 catch (OutOfMemoryException oe)
875 {
876 MessageBox.Show(oe.Message + " - " + _arrayAddFailedAt + " " + i);
877 return fooArray;
878 }
879 catch (Exception ex)
880 {
881 MessageBox.Show(ex.Message + " - " + _arrayAddFailedAt + " " + i);
882 return fooArray;
883 }
884 return fooArray;
885 }
886
887 /// <summary>
888 /// Creates and fills a foo Array with the SAME value for each foo
889 /// </summary>
890 /// <param name="qty">number of foo to add to array</param>
891 /// <returns>foo list of qty specified w/the same values</returns>
892 private Foo[] _getFooArrayWithMatchingValues(int qty)
893 {
894 _arrayMemberIdx = 0;
895 Foo[] fooArray = new Foo[qty];
896 int i = 0;
897 try
898 {
899 for (i = 0; i < qty; i++)
900 fooArray
= new Foo(_fooObjectName + i, _commonValue +
901 _valueProperty);
902 }
903 catch (OutOfMemoryException oe)
904 {
905 MessageBox.Show(oe.Message + " - " + _arrayAddFailedAt + " " + i);
906 _arrayMemberIdx = i;
907 return fooArray;
908 }
909 catch (Exception ex)
910 {
911 MessageBox.Show(ex.Message + " - " + _arrayAddFailedAt + " " + i);
912 return fooArray;
913 }
914 return fooArray;
915 }
916 #endregion
917
918 #region InternalFooObject
919 /// <summary>
920 /// Demo/dummy object with two simple properties: Name and Value
921 /// </summary>
922 internal class Foo
923 {
924 #region Constructors
925 /// <summary>
926 /// Default constructor, sets properties to default values
927 /// </summary>
928 public Foo()
929 {
930 _init();
931 }
932
933 /// <summary>
934 /// Data constructor for, sets properties to passed-in values
935 /// </summary>
936 /// <param name="name">string name</param>
937 /// <param name="value">string value</param>
938 public Foo(string name, string value)
939 {
940 _name = name;
941 _value = value;
942 }
943
944 /// <summary>
945 /// Set default values for the employee object
946 /// </summary>
947 private void _init()
948 {
949 _name = string.Empty;
950 _value = string.Empty;
951 }
952 #endregion
953
954 #region PrivateProps
955 /// <summary>
956 /// local member to store method name
957 /// </summary>
958 private string _name;
959
960 /// <summary>
961 /// local member to store method value
962 /// </summary>
963 private string _value;
964 #endregion
965
966 #region PublicProps
967 /// <summary>
968 /// Read-write property: method value
969 /// </summary>
970 public string Value
971 {
972 get { return _value; }
973 set { _value = value; }
974 }
975 /// <summary>
976 /// Read-write property: method name
977 /// </summary>
978 public string Name
979 {
980 get { return _name; }
981 set { _name = value; }
982 }
983 #endregion
984 }
985 #endregion
986 }
987 }