ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
¸»Ê¿¿µÐ¡ËµÍø ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ46²¿·Ö

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡



It¡¡allows¡¡only¡¡objects¡¡of¡¡type¡¡¡¡Example¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡declare¡¡a¡¡list¡¡like¡¡this£º¡¡



Dim¡¡lst¡¡As¡¡IList£¨Of¡¡Example£©¡¡



you¡¡are¡¡saying¡¡that¡¡the¡¡list¡¡has¡¡a¡¡method¡¡declared¡¡like¡¡this£º¡¡



Sub¡¡Add£¨ByVal¡¡item¡¡As¡¡Example£©¡¡



¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡are¡¡programming¡¡with¡¡Visual¡¡Basic¡¡2008£»¡¡you¡¡should¡¡use¡¡Visual¡¡Basic¡¡2005¡¡and¡¡later¡¡¡¡

collection¡¡classes¡£¡¡The¡¡non¡­¡¡generics¡¡collection¡¡classes¡¡are£»¡¡to¡¡a¡¡large¡¡degree£»¡¡legacy¡¡code¡£¡¡¡¡

Whenever¡¡possible£»¡¡use¡¡¡¡generics¡­based¡¡collection¡¡classes¡£¡¡

¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡you¡¡know¡¡how¡¡to¡¡manage¡¡a¡¡collection¡¡of¡¡objects£»¡¡the¡¡next¡¡section¡¡will¡¡present¡¡a¡¡¡¡

mon¡¡collection¡­related¡¡problem¡¡and¡¡then¡¡solve¡¡the¡¡problem¡£¡¡



Adding¡¡Numbers¡¡and¡¡Finding¡¡Maximum¡¡Values¡¡



Let¡¯s¡¡start¡¡out¡¡with¡¡a¡¡mon¡¡problem£º¡¡addition¡¡of¡¡all¡¡elements¡¡in¡¡a¡¡collection¡£¡¡Consider¡¡the¡¡¡¡

following¡¡code¡£¡¡



Dim¡¡elements¡¡As¡¡IList£¨Of¡¡Integer£©¡¡=¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡



elements¡£Add£¨1£©¡¡

elements¡£Add£¨2£©¡¡

elements¡£Add£¨3£©¡¡



Dim¡¡runningTotal¡¡=¡¡0¡¡

For¡¡Each¡¡value¡¡As¡¡Integer¡¡In¡¡elements¡¡

¡¡¡¡¡¡¡¡runningTotal¡¡=¡¡runningTotal¡¡£«¡¡value¡¡

Next¡¡



¡¡¡¡¡¡¡¡¡¡This¡¡code¡¡has¡¡three¡¡parts£º¡¡initialization¡¡of¡¡elements£»¡¡adding¡¡of¡¡numbers¡¡to¡¡elements£»¡¡and¡¡¡¡

iteration¡¡of¡¡all¡¡values¡¡in¡¡elements¡¡that¡¡are¡¡added¡¡to¡¡the¡¡variable¡¡runningTotal¡£¡¡The¡¡code¡¡seems¡¡¡¡

acceptable¡£¡¡But¡¡let¡¯s¡¡say¡¡that¡¡you¡¡need¡¡to¡¡write¡¡another¡¡piece¡¡of¡¡code¡¡where£»¡¡instead¡¡of¡¡calcu

lating¡¡the¡¡running¡¡total£»¡¡you¡¡want¡¡to¡¡find¡¡the¡¡maximum¡¡value£»¡¡like¡¡this£º¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡258¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

236¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡D¡¡E¡¡L¡¡E¡¡G¡¡A¡¡T¡¡E¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡A¡¡M¡¡B¡¡DA¡¡¡¡¡¡E¡¡X¡¡P¡¡R¡¡E¡¡S¡¡SI¡¡ON¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡elements¡¡As¡¡IList£¨Of¡¡Integer£©¡¡=¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨1£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨2£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨3£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡maxValue¡¡As¡¡Integer¡¡=¡¡Integer¡£MinValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡value¡¡As¡¡Integer¡¡In¡¡elements¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡value¡¡¡·¡¡maxValue¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡maxValue¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡difference¡¡between¡¡the¡¡two¡¡code¡¡pieces¡¡is¡¡the¡¡bolded¡¡code¡£¡¡The¡¡inner¡¡loop¡¡is¡¡different£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡is¡¡a¡¡potential¡¡problem¡£¡¡In¡¡separate¡¡code¡¡files£»¡¡the¡¡repetition¡¡is¡¡not¡¡apparent£»¡¡but¡¡what¡¡if¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡wanted¡¡to¡¡bine¡¡the¡¡code¡¡bases£¿¡¡The¡¡following¡¡code¡¡adds¡¡all¡¡elements¡¡and¡¡finds¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡maximum¡¡value¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡elements¡¡As¡¡IList£¨Of¡¡Integer£©¡¡=¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨1£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨2£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨3£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡runningTotal¡¡As¡¡Integer¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡value¡¡As¡¡Integer¡¡In¡¡elements¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡runningTotal¡¡=¡¡runningTotal¡¡£«¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨RunningTotal¡¡£¨¡¨¡¡&¡¡runningTotal¡¡&¡¡¡¨£©¡¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡maxValue¡¡As¡¡Integer¡¡=¡¡Integer¡£MinValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡value¡¡As¡¡Integer¡¡In¡¡elements¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡value¡¡¡·¡¡maxValue¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡maxValue¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Maximum¡¡value¡¡is¡¡£¨¡¨¡¡&¡¡¡¡maxValue¡¡&¡¡¡¨£©¡¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Another¡¡variation¡¡is¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡elements¡¡As¡¡IList£¨Of¡¡Integer£©¡¡=¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨1£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨2£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨3£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡runningTotal¡¡As¡¡Integer¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡maxValue¡¡As¡¡Integer¡¡=¡¡Integer¡£MinValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡value¡¡As¡¡Integer¡¡In¡¡elements¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡value¡¡¡·¡¡maxValue¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡maxValue¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡runningTotal¡¡=¡¡runningTotal¡¡£«¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡259¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡O¡¡U¡¡T¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡DE¡¡L¡¡E¡¡G¡¡AT¡¡E¡¡S¡¡£»¡¡¡¡¡¡AN¡¡D¡¡¡¡L¡¡A¡¡M¡¡B¡¡D¡¡A¡¡¡¡E¡¡X¡¡PR¡¡E¡¡SSI¡¡O¡¡N¡¡S¡¡¡¡237¡¡



¡¡¡¡¡¡¡¡¡¡For¡¡one¡¡or¡¡two¡¡instances£»¡¡writing¡¡the¡¡For¡¡Each¡¡loop¡¡is¡¡not¡¡that¡¡problematic£»¡¡but¡¡it¡¡would¡¡be¡¡¡¡

if¡¡you¡¡needed¡¡to¡¡use¡¡the¡¡iterator¡¡code¡¡in¡¡a¡¡dozen¡¡places¡£¡¡This¡¡type¡¡of¡¡code¡¡is¡¡harder¡¡to¡¡maintain¡¡¡¡

and¡¡extend¡£¡¡One¡¡way¡¡of¡¡being¡¡more¡¡efficient¡¡is¡¡to¡¡delegate¡¡the¡¡code¡¡to¡¡an¡¡abstract¡¡base¡¡class¡¡¡¡

that¡¡is¡¡implemented¡¡to¡¡calculate¡¡the¡¡running¡¡total¡¡or¡¡maximum¡¡value¡£¡¡The¡¡following¡¡is¡¡the¡¡¡¡

plete¡¡code¡¡£¨you¡¡can¡¡place¡¡the¡¡three¡¡classes¡¡in¡¡separate¡¡files¡¡called¡¡IteratorBaseClass¡£vb£»¡¡¡¡

RunningTotal¡£vb£»¡¡and¡¡MaximumValue¡£vb¡¡if¡¡you¡¡want¡¡to¡¡test¡¡this£©¡£¡¡



¡¡¡¡¡¡¡¡MustInherit¡¡Class¡¡IteratorBaseClass¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Protected¡¡Sub¡¡New£¨ByVal¡¡collection¡¡As¡¡IList£¨Of¡¡Integer£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_collection¡¡=¡¡collection¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡Iterate£¨£©¡¡As¡¡IteratorBaseClass¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡element¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡element¡¡In¡¡Me¡£_collection¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ProcessElement£¨element£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Protected¡¡MustOverride¡¡Sub¡¡ProcessElement£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_collection¡¡As¡¡IList£¨Of¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡Class¡¡RunningTotal¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IteratorBaseClass¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡collection¡¡As¡¡IList£¨Of¡¡Integer£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyBase¡£New£¨collection£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Total¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Protected¡¡Overrides¡¡Sub¡¡ProcessElement£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Total¡¡=¡¡Total¡¡£«¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Total¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡Class¡¡MaximumValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IteratorBaseClass¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡collection¡¡As¡¡IList£¨Of¡¡Integer£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyBase¡£New£¨collection£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MaxValue¡¡=¡¡Integer¡£MinValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡260¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

238¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡D¡¡E¡¡L¡¡E¡¡G¡¡A¡¡T¡¡E¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡A¡¡M¡¡B¡¡DA¡¡¡¡¡¡E¡¡X¡¡P¡¡R¡¡E¡¡S¡¡SI¡¡ON¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Protected¡¡Overrides¡¡Sub¡¡ProcessElement£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨value¡¡¡·¡¡MaxValue£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MaxValue¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡MaxValue¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module¡¡Module1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡elements¡¡As¡¡IList£¨Of¡¡Integer£©¡¡=¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨1£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨2£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elements¡£Add£¨3£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡runningTotal¡¡As¡¡Integer¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TryCast£¨New¡¡RunningTotal£¨elements£©¡£Iterate£¨£©£»¡¡RunningTotal£©¡£Total¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡maximumValue¡¡As¡¡Integer¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TryCast£¨New¡¡MaximumValue£¨elements£©¡£Iterate£¨£©£»¡¡MaximumValue£©¡£MaxValue¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨RunningTotal¡¡£¨¡¨¡¡&¡¡runningTotal¡¡&¡¡¡¨£©¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Maximum¡¡Value¡¡£¨¡¨¡¡&¡¡maximumValue¡¡&¡¡¡¨£©¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡rewritten¡¡code¡¡is¡¡much¡¡longer£»¡¡even¡¡though¡¡the¡¡bolded¡¡code£»¡¡which¡¡represents¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡user¡¡code£»¡¡is¡¡much¡¡shorter¡£¡¡However£»¡¡this¡¡code¡¡still¡¡isn¡¯t¡¡right¡£¡¡The¡¡code¡¡is¡¡ill¡¡fitting¡¡because¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡problem¡¡that¡¡it¡¡addresses¡¡can¡¡be¡¡solved¡¡using¡¡another£»¡¡simpler¡¡technique¡£¡¡So£»¡¡in¡¡a¡¡nutshell£»¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡say¡¡the¡¡problem¡¡is¡¡that¡¡you¡¡want¡¡to¡¡solve¡¡a¡¡single¡¡particular¡¡technical¡¡problem¡¡using¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡elegant¡¡piece¡¡of¡¡code¡¡that¡¡does¡¡not¡¡include¡¡repeated¡¡sections¡¡that¡¡have¡¡been¡¡copied¡¡and¡¡pasted¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡A¡¡better¡¡solution¡¡is¡¡consider¡¡the¡¡code¡¡as¡¡two¡¡code¡¡blocks£»¡¡and¡¡in¡¡the¡¡following¡¡section£»¡¡you¡¯ll¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡see¡¡how¡¡delegates¡¡can¡¡solve¡¡the¡¡problem¡¡of¡¡adding¡¡and¡¡keeping¡¡a¡¡running¡¡total¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡WEIGHING¡¡THE¡¡ADVANTAGES¡¡OF¡¡REUSING¡¡CODE¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Very¡¡often£»¡¡when¡¡you¡¡write¡¡code£»¡¡the¡¡code¡¡that¡¡performs¡¡the¡¡task¡¡directly¡¡is¡¡shorter¡¡and¡¡to¡¡the¡¡point¡£¡¡When¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡abstract¡¡the¡¡code¡¡and¡¡develop¡¡general¡¡classes£»¡¡the¡¡code¡¡will¡¡begin¡¡to¡¡bloat¡¡and¡¡expand£»¡¡but¡¡the¡¡advantage¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡the¡¡code¡¡can¡¡be¡¡reused¡£¡¡So£»¡¡when¡¡is¡¡abstracting¡¡code¡¡worth¡¡the¡¡effort£¿¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Consider¡¡the¡¡analogy¡¡of¡¡building¡¡a¡¡house¡£¡¡You¡¡are¡¡constructing¡¡the¡¡trusses¡¡for¡¡the¡¡house¡£¡¡You¡¡have¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡blueprint¡¡that¡¡indicates¡¡that¡¡you¡¡need¡¡to¡¡build¡¡50¡¡trusses¡£¡¡You¡¡could¡¡build¡¡each¡¡of¡¡the¡¡50¡¡trusses¡¡individually£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡you¡¡could¡¡build¡¡a¡¡jig¡¡to¡¡speed¡¡up¡¡building¡¡the¡¡trusses¡£¡¡And¡¡herein¡¡lies¡¡the¡¡problem¡£¡¡If¡¡the¡¡trusses¡¡can¡¡be¡¡built¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡without¡¡a¡¡jig¡¡in¡¡10¡¡hours£»¡¡and¡¡with¡¡the¡¡jig¡¡in¡¡2¡¡hours£»¡¡you¡¡would¡¡think¡¡building¡¡the¡¡jig¡¡was¡¡a¡¡good¡¡idea¡£¡¡But¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡so¡¡fast¡£¡¡What¡¡if¡¡building¡¡the¡¡jig¡¡takes¡¡20¡¡hours£¿¡¡Then¡¡the¡¡time¡¡that¡¡you¡¡saved¡¡by¡¡using¡¡the¡¡jig¡¡you¡¡lost¡¡by¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡building¡¡the¡¡jig¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡261¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡O¡¡U¡¡T¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡DE¡¡L¡¡E¡¡G¡¡AT¡¡E¡¡S¡¡£»¡¡¡¡¡¡AN¡¡D¡¡¡¡L¡¡A¡¡M¡¡B¡¡D¡¡A¡¡¡¡E¡¡X¡¡PR¡¡E¡¡SSI¡¡O¡¡N¡¡S¡¡¡¡239¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Software¡¡is¡¡no¡¡different¡£¡¡Sometimes£»¡¡even¡¡though¡¡the¡¡code¡¡is¡¡more¡¡plicated¡¡and¡¡bloated£»¡¡if¡¡the¡¡code¡¡¡¡

¡¡¡¡¡¡is¡¡reused¡¡often¡¡enough£»¡¡abstracting¡¡it¡¡saves¡¡time£»¡¡as¡¡the¡¡end¡­user¡¡code¡¡is¡¡simplified¡£¡¡Experience¡¡will¡¡tell¡¡you¡¡¡¡

¡¡¡¡¡¡when¡¡to¡¡code¡¡specifically¡¡or¡¡when¡¡to¡¡write¡¡general¡¡code¡¡that¡¡can¡¡be¡¡reused¡£¡¡A¡¡rule¡¡of¡¡thumb¡¡is¡¡to¡¡start¡¡out¡¡by¡¡¡¡

¡¡¡¡¡¡solving¡¡the¡¡problem£»¡¡and¡¡if¡¡it¡¡looks¡¡like¡¡the¡¡code¡¡can¡¡be¡¡reused£»¡¡then¡¡abstract¡¡the¡¡specific¡¡code¡£¡¡¡¡



Using¡¡Delegates¡¡



Since¡¡the¡¡beginning¡¡of¡¡Visual¡¡Basic£»¡¡there¡¡has¡¡been¡¡a¡¡concept¡¡called¡¡a¡¡delegate¡£¡¡A¡¡delegate¡¡is¡¡a¡¡¡¡

type¡¡that¡¡represents¡¡a¡¡method¡¡signature¡£¡¡For¡¡example£»¡¡consider¡¡the¡¡following¡¡interface¡¡definition¡£¡¡



Interface¡¡IExample¡¡¡¡

¡¡¡¡¡¡¡¡Sub¡¡Method£¨£©¡¡

End¡¡Interface¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡equivalent¡¡delegate¡¡would¡¡look¡¡like¡¡this£º¡¡



Delegate¡¡Sub¡¡Method£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡A¡¡delegate¡¡and¡¡interface¡¡can¡¡share¡¡the¡¡same¡¡role£»¡¡in¡¡that¡¡they¡¡are¡¡two¡¡different¡¡ways¡¡to¡¡¡¡

represent¡¡the¡¡signature¡¡for¡¡a¡¡method¡¡that¡¡you¡¡implement¡¡elsewhere¡£¡¡An¡¡interface¡¡can¡¡have¡¡¡¡

multiple¡¡methods¡¡and¡¡properties¡£¡¡A¡¡delegate¡¡is¡¡a¡¡method¡¡signature¡¡and¡¡can¡¡define¡¡only¡¡the¡¡¡¡

parameters¡¡and¡¡return¡¡types¡¡for¡¡that¡¡method¡¡signature¡£¡¡The¡¡purpose¡¡of¡¡delegates¡¡is¡¡to¡¡be¡¡able¡¡¡¡

to¡¡define¡¡a¡¡generic¡¡method¡­calling¡¡mechanism¡¡without¡¡needing¡¡to¡¡add¡¡the¡¡baggage¡¡of¡¡imple

menting¡¡an¡¡interface¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡approach¡¡used¡¡in¡¡the¡¡delegate¡¡solution¡¡to¡¡the¡¡problem¡¡presented¡¡in¡¡the¡¡previous¡¡section¡¡¡¡

is¡¡to¡¡define¡¡a¡¡chunk¡¡of¡¡functionality¡¡that¡¡performs¡¡the¡¡iteration£»¡¡called¡¡an¡¡¡¡iterator¡£¡¡And¡¡then¡¡to¡¡¡¡

do¡¡something¡¡with¡¡the¡¡iteration£»¡¡another¡¡chunk¡¡of¡¡functionality¡¡is¡¡integrated¡¡via¡¡a¡¡delegate¡£¡¡

¡¡¡¡¡¡¡¡¡¡Following¡¡is¡¡the¡¡plete¡¡rewritten¡¡For¡¡Each¡¡code¡¡that¡¡uses¡¡delegates¡£¡¡The¡¡entire¡¡solution¡¡¡¡

is¡¡shown¡¡for¡¡a¡¡big¡¡picture¡¡purposes£»¡¡and¡¡then¡¡will¡¡be¡¡split¡¡into¡¡smaller¡¡chunks¡¡for¡¡explanation¡¡¡¡

purposes¡£¡¡



Imports¡¡System¡£Runtime¡£pilerServices¡¡



Namespace¡¡DelegateImplementation¡¡

¡¡¡¡¡¡¡¡Delegate¡¡Sub¡¡ProcessValue£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡



¡¡¡¡¡¡¡¡Module¡¡Iterator¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡¡Methods¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Iterate£¨ByVal¡¡collection¡¡As¡¡ICollection£¨Of¡¡Integer£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡cb¡¡As¡¡ProcessValue£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡element¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡element¡¡In¡¡collection¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cb£¨element£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡End¡¡Module¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡262¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

240¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡D¡¡E¡¡L¡¡E¡¡G¡¡A¡¡T¡¡E¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡A¡¡M¡¡B¡¡DA¡¡¡¡¡¡E¡¡X¡¡P¡¡R¡¡E¡¡S¡¡SI¡¡ON¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module¡¡Tests¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡¡Fields¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_maxValue¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_runningTotal¡¡As¡¡Integer¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Sub¡¡ProcessMaximumValue£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨value¡¡¡·¡¡Tests¡£_maxValue£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_maxValue¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Sub¡¡ProcessRunningTotal£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_runningTotal¡¡=¡¡_runningTotal¡¡£«¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡RunAll£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡lst¡¡As¡¡New¡¡List£¨Of¡¡Integer£©£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lst¡£Add£¨1£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lst¡£Add£¨2£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lst¡£Add£¨3£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lst¡£Add£¨4£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_runningTotal¡¡=¡¡0¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Iterator¡£Iterate£¨lst£»¡¡New¡¡ProcessValue£¨AddressOf¡¡ProcessRunningTotal£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨£¨¡¨Running¡¡total¡¡is¡¡£¨¡¨¡¡&¡¡_runningTotal¡¡&¡¡¡¨£©¡¨£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_maxValue¡¡=¡¡Integer¡£MinValue¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Iterator¡£Iterate£¨lst£»¡¡New¡¡ProcessValue£¨AddressOf¡¡ProcessMaximumValue£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨£¨¡¨Maximum¡¡value¡¡is¡¡£¨¡¨¡¡&¡¡_maxValue¡¡&¡¡¡¨£©¡¨£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Module¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Namespace¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaring¡¡the¡¡Delegate¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡beginning¡¡of¡¡the¡¡code¡¡is¡¡the¡¡single¡¡line¡¡that¡¡contains¡¡the¡¡Delegate¡¡keyword¡£¡¡Any¡¡delegate¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementations¡¡must¡¡match¡¡this¡¡signature¡¡£¨in¡¡our¡¡case£»¡¡ProcessMaximumValue£¨£©¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ProcessRunningTotal£¨£©¡¡match¡¡the¡¡delegate¡¡signature£©£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Delegate¡¡Sub¡¡ProcessValue£¨ByVal¡¡value¡¡As¡¡Integer£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡declaration¡¡of¡¡the¡¡delegate¡¡is¡¡outside¡¡the¡¡scope¡¡of¡¡a¡¡class¡¡or¡¡interface£»¡¡but¡¡the¡¡usage¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡delegate¡¡must¡¡be¡¡in¡¡the¡¡context¡¡of¡¡a¡¡class¡¡£¨as¡¡it¡¡is¡¡in¡¡our¡¡case£©¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡type¡¡of¡¡the¡¡delegate¡¡is¡¡the¡¡name¡¡of¡¡the¡¡method£»¡¡which¡¡is¡¡ProcessValue¡¡in¡¡our¡¡case¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡delegate¡¡will¡¡be¡¡used¡¡in¡¡the¡¡code¡¡example¡¡to¡¡provide¡¡a¡¡general¡¡callback¡¡mechanism¡¡in¡¡the¡¡iterator¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡iterator¡¡is¡¡declared¡¡as¡¡follows£º¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡263¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡O¡¡U¡¡T¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡DE¡¡L¡¡E¡¡G¡¡AT¡¡E¡¡S¡¡£»¡¡¡¡¡¡AN¡¡D¡¡¡¡L¡¡A¡¡M¡¡B¡¡D¡¡A¡¡¡¡E¡¡X¡¡PR¡¡E¡¡SSI¡¡O¡¡N¡¡S¡¡¡¡241¡¡



¡¡¡¡¡¡¡¡Module¡¡Extensions¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Iterate£¨ByVal¡¡collection¡¡As¡¡ICollection£¨Of¡¡Integer£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡cb¡¡As¡¡ProcessValue£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡element¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡element¡¡In¡¡collection¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cb£¨element£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡A¡¡module¡¡method¡¡can¡¡be¡¡called¡¡as¡¡follows£º¡¡



Iterator¡£Iterate£¨lst£»¡¡delegate£©¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡parameter¡¡of¡¡the¡¡Iterate£¨£©¡¡method¡¡is¡¡the¡¡list¡¡to¡¡iterate£»¡¡and¡¡the¡¡second¡¡parameter¡¡¡¡

is¡¡a¡¡delegate¡¡instance¡¡that¡¡matches¡¡the¡¡method¡¡signature¡¡of¡¡the¡¡ProcessValue¡¡delegate¡£¡¡

¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡implementation¡¡of¡¡Iterate£¨£©£»¡¡each¡¡element¡¡of¡¡the¡¡collection¡¡is¡¡iterated¡¡using¡¡For¡¡¡¡

Each£»¡¡and¡¡then¡¡in¡¡the¡¡loop£»¡¡the¡¡variable¡¡cb¡¡is¡¡called¡¡as¡¡if¡¡it¡¡were¡¡a¡¡method¡£¡¡The¡¡calling¡¡of¡¡cb¡¡is¡¡¡¡

what¡¡separates¡¡the¡¡iterator¡¡from¡¡the¡¡processing¡¡of¡¡the¡¡iteration¡£¡¡Imagine¡¡having¡¡implemented¡¡¡¡

a¡¡method¡¡that¡¡calculates¡¡the¡¡running¡¡total¡¡or¡¡maximum¡£¡¡To¡¡iterate¡¡all¡¡of¡¡the¡¡elements£»¡¡you¡¡¡¡

would¡¡instantiate¡¡the¡¡delegate¡¡with¡¡the¡¡method¡¡and¡¡call¡¡Iterate£¨£©£»¡¡as¡¡follows£º¡¡



Iterator¡£Iterate£¨lst£»¡¡New¡¡ProcessValue£¨ProcessRunningTotal£©£©¡¡

¡£¡¡¡£¡¡¡£¡¡

Iterator¡£
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨11£© ²È£¨11£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡