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

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

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


the¡¡property¡¡¡¡Length¡¡to¡¡the¡¡¡¡LightingController¡¡class¡£¡¡Another¡¡approach¡¡is¡¡to¡¡use¡¡¡¡For¡¡Each£»¡¡as¡¡¡¡

follows£º¡¡



For¡¡Each¡¡rg¡¡As¡¡RoomGrouping¡¡In¡¡controller¡£RoomGroupingIterator£¨£©¡¡

¡¡¡¡¡¡¡¡'¡¡Do¡¡something¡¡with¡¡the¡¡rg¡¡

¡¡¡¡¡¡¡¡Console¡£WriteLine£¨rg¡£Description£©¡¡

Next¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡¡¡For¡¡Each¡¡syntax¡¡is¡¡simpler¡£¡¡It¡¡doesn¡¯t¡¡matter¡¡that¡¡we¡¯ve¡¡lost¡¡the¡¡information¡¡about¡¡¡¡

which¡¡offset¡¡is¡¡which¡¡description£»¡¡because¡¡that¡¡information¡¡is¡¡useless¡£¡¡Remember¡¡we¡¡are¡¡dealing¡¡¡¡

with¡¡a¡¡linked¡¡list¡¡that¡¡can¡¡change¡¡its¡¡order¡¡however¡¡it¡¡pleases¡£¡¡Thus£»¡¡having¡¡a¡¡numeric¡¡identifier¡¡¡¡

is¡¡pletely¡¡meaningless¡£¡¡The¡¡only¡¡reliable¡¡way¡¡to¡¡find¡¡a¡¡room¡¡grouping¡¡is¡¡to¡¡know¡¡its¡¡description¡¡¡¡

or¡¡hold¡¡a¡¡specific¡¡index¡¡for¡¡the¡¡collection¡£¡¡



¡öNote¡¡¡¡Unless¡¡you¡¡are¡¡absolutely¡¡sure¡¡that¡¡the¡¡collection¡¡you¡¡are¡¡manipulating¡¡does¡¡not¡¡move¡¡elements¡¡¡¡

around£»¡¡holding¡¡an¡¡index¡¡as¡¡a¡¡unique¡¡description¡¡of¡¡the¡¡object¡¡can¡¡be¡¡dangerous£»¡¡and¡¡potentially¡¡could¡¡corrupt¡¡¡¡

the¡¡state¡¡of¡¡an¡¡application¡£¡¡In¡¡this¡¡chapter£»¡¡I¡¡have¡¡illustrated¡¡two¡¡other¡¡techniques¡¡that¡¡can¡¡be¡¡used¡¡to¡¡refer

ence¡¡a¡¡particular¡¡object£º¡¡a¡¡handle¡¡and¡¡a¡¡default¡¡property¡£¡¡



¡¡¡¡¡¡¡¡¡¡We¡¯ll¡¡e¡¡to¡¡the¡¡¡¡RoomGroupingIterator£¨£©¡¡method¡¡in¡¡a¡¡bit£»¡¡but¡¡first¡¡we¡¡need¡¡to¡¡see¡¡the¡¡¡¡

enumerable¡¡that¡¡it¡¡returns¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡¡¡LightingController¡¡class¡¡has¡¡no¡¡For¡¡Each¡¡functionality¡¡built¡¡in¡£¡¡To¡¡give¡¡it¡¡this¡¡function

ality£»¡¡you¡¡need¡¡to¡¡create¡¡a¡¡class¡¡that¡¡can¡¡be¡¡enumerated¡¡and¡¡a¡¡class¡¡that¡¡can¡¡be¡¡an¡¡enumerator£»¡¡¡¡

which¡¡is¡¡a¡¡class¡¡that¡¡can¡¡perform¡¡the¡¡iteration¡£¡¡The¡¡plete¡¡implementation¡¡of¡¡a¡¡class¡¡that¡¡¡¡

can¡¡be¡¡enumerated¡¡for¡¡BaseLinkedListItem¡¡is¡¡as¡¡follows¡¡£¨it¡¡is¡¡also¡¡the¡¡enumerator¡¡class£»¡¡as¡¡¡¡

you¡¯ll¡¡see£©¡£¡¡



Public¡¡Class¡¡LinkedListEnumerable¡¡

¡¡¡¡¡¡¡¡Implements¡¡IEnumerable£»¡¡IEnumerator¡¡

¡¡¡¡¡¡¡¡Private¡¡_currNode¡¡As¡¡BaseLinkedListItem¡¡

¡¡¡¡¡¡¡¡Private¡¡_firstNode¡¡As¡¡BaseLinkedListItem¡¡



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡linkedList¡¡As¡¡BaseLinkedListItem£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_currNode¡¡=¡¡linkedList¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_firstNode¡¡=¡¡linkedList¡¡

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



¡¡¡¡¡¡¡¡Public¡¡Function¡¡GetEnumerator£¨£©¡¡As¡¡IEnumerator¡¡_¡¡

¡¡¡¡¡¡¡¡Implements¡¡IEnumerable¡£GetEnumerator¡¡

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

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡240¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

218¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Current£¨£©¡¡As¡¡Object¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡IEnumerator¡£Current¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_currNode¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡MoveNext£¨£©¡¡As¡¡Boolean¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡IEnumerator¡£MoveNext¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_currNode¡¡Is¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_currNode¡¡=¡¡_currNode¡£NextItem¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_currNode¡¡Is¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡True¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Reset£¨£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡IEnumerator¡£Reset¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_currNode¡¡=¡¡_firstNode¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡A¡¡class¡¡that¡¡can¡¡be¡¡enumerated¡¡must¡¡fulfill¡¡two¡¡objectives£º¡¡implement¡¡the¡¡¡¡IEnumerable¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡interface¡¡and¡¡implement¡¡the¡¡IEnumerator¡¡interface¡£¡¡The¡¡IEnumerable¡¡class¡¡has¡¡a¡¡single¡¡method£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡GetEnumerator£¨£©£»¡¡which¡¡is¡¡used¡¡to¡¡retrieve¡¡a¡¡class¡¡instance¡¡that¡¡implements¡¡the¡¡IEnumerator¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡interface¡£¡¡The¡¡two¡¡interfaces¡¡are¡¡split¡¡because¡¡retrieving¡¡the¡¡iteration¡¡functionality¡¡and¡¡iter

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ating¡¡the¡¡collection¡¡are¡¡two¡¡separate¡¡steps¡£¡¡However£»¡¡in¡¡the¡¡case¡¡of¡¡¡¡LinkedListEnumerable£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡these¡¡steps¡¡are¡¡bined¡¡into¡¡one¡¡class£»¡¡and¡¡that¡¡is¡¡often¡¡the¡¡case¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡best¡¡way¡¡to¡¡explain¡¡how¡¡¡¡LinkedListEnumerable¡¡works¡¡is¡¡to¡¡go¡¡through¡¡the¡¡code¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡explain¡¡what¡¡methods¡¡and¡¡properties¡¡are¡¡called¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡The¡¡code¡¡in¡¡the¡¡client¡¡application¡¡starts¡¡a¡¡For¡¡Each¡¡loop£»¡¡and¡¡sets¡¡up¡¡a¡¡context¡¡where¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡collection¡¡of¡¡elements¡¡is¡¡being¡¡iterated¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡The¡¡code¡¡calls¡¡the¡¡collection¡¡iterator¡£¡¡In¡¡the¡¡example£»¡¡this¡¡means¡¡calling¡¡the¡¡method¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡RoomGroupingIterator£¨£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡RoomGroupingIterator£¨£©¡¡returns¡¡an¡¡instance¡¡of¡¡¡¡LinkedListEnumerable£»¡¡which¡¡is¡¡assigned¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡linked¡¡list¡¡that¡¡will¡¡be¡¡iterated¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡RoomGroupingIterator£¨£©¡¡As¡¡IEnumerable¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡New¡¡LinkedListEnumerable£¨_roomGroupings£©¡¡

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡241¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡H¡¡AP¡¡TE¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡T¡¡O¡¡R¡¡IE¡¡N¡¡TE¡¡D¡¡¡¡¡¡A¡¡R¡¡CH¡¡I¡¡TE¡¡C¡¡TU¡¡R¡¡E¡¡219¡¡



¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡The¡¡method¡¡¡¡LinkedListEnumerable¡£GetEnumerator£¨£©¡¡is¡¡called£»¡¡and¡¡an¡¡instance¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡IEnumerator¡¡is¡¡returned¡£¡¡



¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡The¡¡code¡¡calls¡¡the¡¡method¡¡¡¡LinkedListEnumerable¡£MoveNext£¨£©¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡6¡£¡¡¡¡The¡¡implementation¡¡of¡¡MoveNext£¨£©¡¡returns¡¡True¡¡to¡¡indicate¡¡that¡¡it¡¡was¡¡possible¡¡to¡¡move¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡next¡¡element¡£¡¡If¡¡¡¡False¡¡is¡¡returned£»¡¡it¡¡means¡¡that¡¡the¡¡end¡¡of¡¡the¡¡collection¡ªor¡¡in¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this¡¡case£»¡¡the¡¡linked¡¡list¡ªis¡¡reached¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡7¡£¡¡¡¡If¡¡MoveNext£¨£©¡¡returns¡¡True£»¡¡then¡¡the¡¡property¡¡LinkedListEnumerable¡£Current£¨£©¡¡is¡¡called¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡retrieve¡¡the¡¡current¡¡linked¡¡list¡¡element¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡8¡£¡¡¡¡The¡¡retrieved¡¡linked¡¡list¡¡element¡¡is¡¡assigned¡¡to¡¡the¡¡variable¡¡of¡¡the¡¡For¡¡Each¡¡loop£»¡¡which¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡rg¡¡in¡¡this¡¡example¡£¡¡



¡¡¡¡¡¡¡¡¡¡9¡£¡¡¡¡Control¡¡is¡¡returned¡¡to¡¡the¡¡For¡¡Each¡¡loop£»¡¡and¡¡the¡¡user¡¡code¡¡does¡¡something¡¡with¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡linked¡¡list¡¡element¡£¡¡¡¡



¡¡¡¡¡¡¡¡10¡£¡¡¡¡When¡¡the¡¡For¡¡Each¡¡loop¡¡attempts¡¡another¡¡iteration£»¡¡steps¡¡5¡¡through¡¡9¡¡are¡¡repeated¡¡until¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MoveNext£¨£©¡¡returns¡¡¡¡False¡£¡¡



¡¡¡¡¡¡¡¡11¡£¡¡¡¡When¡¡MoveNext£¨£©¡¡returns¡¡¡¡False£»¡¡the¡¡iterator¡¡exits£»¡¡causing¡¡an¡¡exit¡¡of¡¡the¡¡For¡¡Each¡¡loop¡£¡¡



Adding¡¡Rooms¡¡to¡¡Groupings¡¡



The¡¡data¡¡handle¡¡that¡¡we¡¡defined¡¡when¡¡adding¡¡the¡¡grouping¡¡is¡¡used¡¡when¡¡we¡¡add¡¡a¡¡room¡¡to¡¡a¡¡¡¡

grouping¡£¡¡The¡¡idea¡¡of¡¡the¡¡handle¡¡is¡¡to¡¡provide¡¡a¡¡reference¡¡that¡¡the¡¡kernel¡¡can¡¡use¡£¡¡Since¡¡the¡¡¡¡

handle¡¡is¡¡an¡¡instance¡¡of¡¡a¡¡RoomGrouping¡¡type£»¡¡whenever¡¡a¡¡room¡¡is¡¡added¡¡to¡¡a¡¡grouping¡¡based¡¡on¡¡¡¡

a¡¡handle£»¡¡it¡¡is¡¡not¡¡necessary¡¡to¡¡find¡¡the¡¡room¡¡grouping¡£¡¡The¡¡handle¡¡is¡¡the¡¡room¡¡grouping£»¡¡and¡¡¡¡

all¡¡that¡¡is¡¡necessary¡¡is¡¡a¡¡type¡¡cast¡£¡¡The¡¡following¡¡demonstrates¡¡how¡¡to¡¡add¡¡a¡¡room¡¡to¡¡a¡¡room¡¡¡¡

grouping¡¡£¨in¡¡¡¡LightingController£©¡£¡¡



Public¡¡Sub¡¡AddRoomToGrouping£¨ByVal¡¡grouping¡¡As¡¡Object£»¡¡ByVal¡¡room¡¡As¡¡IRoom£©¡¡

¡¡¡¡¡¡¡¡Dim¡¡roomGrouping¡¡As¡¡RoomGrouping¡¡=¡¡TryCast£¨grouping£»¡¡RoomGrouping£©¡¡

¡¡¡¡¡¡¡¡If¡¡roomGrouping¡¡Is¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡New¡¡Exception£¨¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨Handle¡¡grouping¡¡is¡¡not¡¡a¡¡valid¡¡room¡¡grouping¡¡instance¡¨£©¡¡

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

¡¡¡¡¡¡¡¡Dim¡¡oldRooms¡¡As¡¡Room¡¡=¡¡TryCast£¨roomGrouping¡£Rooms£»¡¡Room£©¡¡

¡¡¡¡¡¡¡¡If¡¡¡¡oldRooms¡¡Is¡¡Nothing¡¡Then¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡roomGrouping¡£Rooms¡¡=¡¡New¡¡Room£¨£©¡¡With¡¡£û¡£ObjRoom¡¡=¡¡room£ý¡¡

¡¡¡¡¡¡¡¡Else¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡roomGrouping¡£Rooms¡£Insert£¨New¡¡Room£¨£©¡¡With¡¡£û¡£ObjRoom¡¡=¡¡room£ý£©¡¡

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

End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡implementation¡¡of¡¡AddRoomToGrouping£¨£©£»¡¡the¡¡first¡¡step¡¡is¡¡to¡¡cast¡¡the¡¡handle¡¡grouping¡¡¡¡

to¡¡an¡¡instance¡¡of¡¡RoomGrouping¡£¡¡The¡¡cast¡¡used¡¡is¡¡the¡¡TryCast£¨£©¡¡function£»¡¡so¡¡that¡¡if¡¡the¡¡cast¡¡fails£»¡¡¡¡

it¡¡is¡¡only¡¡necessary¡¡to¡¡test¡¡if¡¡roomGrouping¡¡is¡¡not¡¡Nothing¡£¡¡Executing¡¡the¡¡Nothing¡¡test¡¡is¡¡absolutely¡¡¡¡

vital£»¡¡otherwise£»¡¡you¡¡might¡¡perform¡¡operations¡¡that¡¡will¡¡cause¡¡an¡¡exception¡¡to¡¡be¡¡thrown¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡242¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

220¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡the¡¡handle¡¡has¡¡been¡¡cast¡¡to¡¡a¡¡RoomGrouping¡¡instance£»¡¡adding¡¡a¡¡room¡¡to¡¡the¡¡linked¡¡list¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡trivial¡£¡¡Adding¡¡a¡¡room¡¡involves¡¡only¡¡assigning¡¡the¡¡head¡¡of¡¡the¡¡list¡¡if¡¡there¡¡are¡¡no¡¡rooms¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡list£»¡¡or¡¡calling¡¡the¡¡method¡¡Insert£¨£©¡¡if¡¡there¡¡are¡¡rooms¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Performing¡¡Operations¡¡on¡¡a¡¡Group¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡a¡¡grouping¡¡defined£»¡¡you¡¡can¡¡perform¡¡global¡¡operations¡¡that¡¡affect¡¡all¡¡rooms¡¡of¡¡a¡¡grouping¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡One¡¡example¡¡is¡¡turning¡¡off¡¡the¡¡lights¡¡in¡¡all¡¡the¡¡rooms¡¡in¡¡a¡¡grouping£»¡¡which¡¡is¡¡based¡¡on¡¡the¡¡IRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡interface¡¡instance¡£¡¡Here¡¡is¡¡the¡¡code¡¡to¡¡turn¡¡off¡¡all¡¡of¡¡the¡¡lights¡¡in¡¡a¡¡grouping£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TurnOffLights£¨ByVal¡¡grouping¡¡As¡¡Object£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡enumerableGrouping¡¡As¡¡LinkedListEnumerable¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡LinkedListEnumerable£¨TryCast£¨grouping£»¡¡BaseLinkedListItem£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡room¡¡As¡¡IRoom¡¡In¡¡enumerableGrouping¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡remote¡¡As¡¡IRemoteControlRoom¡¡=¡¡TryCast£¨room£»¡¡IRemoteControlRoom£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡sensorRoom¡¡As¡¡ISensorRoom¡¡=¡¡TryCast£¨room£»¡¡ISensorRoom£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡sensorRoom¡¡IsNot¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡Not¡¡sensorRoom¡£IsPersonInRoom¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Continue¡¡For¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡remote¡¡IsNot¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡remote¡£LightSwitch£¨False£©¡¡

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

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Notice¡¡that¡¡the¡¡handle¡¡is¡¡not¡¡converted¡¡into¡¡a¡¡RoomGrouping¡¡instance¡£¡¡The¡¡handle¡¡is¡¡type¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cast¡¡to¡¡BaseLinkedListItem£»¡¡and¡¡then¡¡passed¡¡to¡¡the¡¡LinkedListEnumerable¡¡constructor¡£¡¡For¡¡each¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡iteration¡¡of¡¡the¡¡For¡¡Each¡¡loop£»¡¡the¡¡IRoom¡¡instance¡¡room¡¡is¡¡cast¡¡into¡¡the¡¡types¡¡IRemoteControlRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡ISensorRoom¡£¡¡A¡¡cast¡¡to¡¡both¡¡of¡¡these¡¡types¡¡is¡¡necessary¡¡because£»¡¡depending¡¡on¡¡the¡¡room¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type£»¡¡certain¡¡algorithms¡¡need¡¡to¡¡be¡¡executed¡£¡¡For¡¡example£»¡¡if¡¡the¡¡room¡¡is¡¡of¡¡type¡¡ISensorRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡the¡¡property¡¡IsPersonInRoom¡¡is¡¡True£»¡¡then¡¡the¡¡lights¡¡should¡¡be¡¡left¡¡as¡¡is¡£¡¡If¡¡the¡¡lights¡¡are¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡left¡¡as¡¡is£»¡¡that¡¡means¡¡performing¡¡the¡¡next¡¡iteration¡¡using¡¡the¡¡Continue¡¡For¡¡keywords¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡processing¡¡continues£»¡¡we¡¡check¡¡if¡¡the¡¡room¡¡can¡¡be¡¡remotely¡¡controlled£»¡¡which¡¡means¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡it¡¡implements¡¡the¡¡interface¡¡IRemoteControlRoom¡£¡¡If¡¡remote¡¡is¡¡not¡¡Nothing£»¡¡then¡¡we¡¡can¡¡call¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡LightSwitch£¨£©¡¡method¡¡with¡¡a¡¡parameter¡¡of¡¡¡¡False¡¡to¡¡turn¡¡off¡¡the¡¡lights¡£¡¡The¡¡iteration¡¡continues¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡for¡¡all¡¡rooms¡¡in¡¡the¡¡grouping¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡pletes¡¡the¡¡kernel£»¡¡but¡¡before¡¡you¡¡how¡¡it¡¡fits¡¡with¡¡a¡¡lighting¡¡application£»¡¡I¡¡would¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡like¡¡to¡¡discuss¡¡an¡¡alternative¡¡approach¡¡to¡¡implementing¡¡the¡¡kernel¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Defining¡¡the¡¡Kernel¡¡As¡¡an¡¡Interface¡¡Instead¡¡of¡¡a¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡I¡¡noted¡¡earlier£»¡¡rather¡¡than¡¡defining¡¡the¡¡kernel¡¡as¡¡a¡¡class£»¡¡another¡¡approach¡¡would¡¡be¡¡to¡¡define¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡kernel¡¡as¡¡an¡¡interface¡¡that¡¡is¡¡implemented¡£¡¡If¡¡a¡¡pany¡¡were¡¡to¡¡distribute¡¡multiple¡¡implemen

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tations¡¡of¡¡a¡¡controller£»¡¡an¡¡interface¡¡would¡¡be¡¡appropriate£»¡¡but¡¡only¡¡if¡¡the¡¡multiple¡¡implementations¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡interface¡¡used¡¡the¡¡same¡¡set¡¡of¡¡methods¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡243¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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