°Ë±¦Êé¿â > ÎÄѧÆäËûµç×ÓÊé > VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) >

µÚ63²¿·Ö

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

С˵£º VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) ×ÖÊý£º ÿҳ4000×Ö

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡




¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡indicating¡¡the¡¡RoomGrouping¡¡instance¡¡you¡¡are¡¡referring¡¡to¡¡in¡¡the¡¡list¡£¡¡However£»¡¡that¡¡would¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mean¡¡having¡¡access¡¡to¡¡the¡¡list¡¡somewhere£»¡¡and¡¡then¡¡needing¡¡to¡¡iterate¡¡to¡¡find¡¡the¡¡appropriate¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡RoomGrouping¡¡instance¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡solution¡¡is¡¡to¡¡declare¡¡the¡¡method¡¡as¡¡returning¡¡a¡¡type¡¡Object¡£¡¡When¡¡you¡¡use¡¡Object£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡are¡¡defining¡¡that¡¡your¡¡method¡¡is¡¡giving¡¡you¡¡an¡¡object¡¡instance¡£¡¡The¡¡caller¡¡may¡¡or¡¡may¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡know¡¡what¡¡the¡¡instance¡¡type¡¡is£»¡¡and¡¡in¡¡the¡¡case¡¡of¡¡AddRoomGrouping£¨£©£»¡¡it¡¡doesn¡¯t¡£¡¡But¡¡that¡¡is¡¡fine£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡because¡¡you£»¡¡as¡¡the¡¡user£»¡¡will¡¡consider¡¡the¡¡instance¡¡as¡¡a¡¡key¡¡that¡¡is¡¡managed¡¡by¡¡the¡¡class¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡LightingController¡£¡¡In¡¡technical¡¡jargon£»¡¡the¡¡object¡¡instance¡¡is¡¡a¡¡handle¡¡that¡¡you¡¡hold¡¡and¡¡pass¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡some¡¡other¡¡ponent¡¡that¡¡knows¡¡what¡¡to¡¡do¡¡with¡¡it¡£¡¡In¡¡the¡¡example£»¡¡it¡¡means¡¡giving¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡handle¡¡to¡¡¡¡LightingController¡¡because¡¡it¡¡knows¡¡that¡¡the¡¡handle¡¡is¡¡an¡¡instance¡¡of¡¡RoomGrouping¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡237¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡215¡¡



¡öNote¡¡¡¡Handles¡¡were¡¡very¡¡popular¡¡in¡¡the¡¡C¡¡programming¡¡days¡¡and¡¡were¡¡consider¡¡pointers¡¡to¡¡memory¡£¡¡The¡¡¡¡

caller¡¡did¡¡not¡¡know¡¡what¡¡the¡¡pointer¡¡pointed¡¡to£»¡¡but¡¡kept¡¡using¡¡it¡¡when¡¡interacting¡¡with¡¡an¡¡API¡£¡¡These¡¡days£»¡¡¡¡

handles¡¡have¡¡lost¡¡significance¡¡as¡¡we¡¡have¡¡objects£»¡¡¡¡generics£»¡¡and¡¡other¡¡programming¡¡constructs¡£¡¡However£»¡¡at¡¡¡¡

times£»¡¡handles¡¡are¡¡very¡¡useful¡£¡¡They¡¡can¡¡help¡¡you¡¡to¡¡avoid¡¡the¡¡problem¡¡of¡¡having¡¡to¡¡expose¡¡the¡¡internal¡¡state¡¡¡¡

of¡¡your¡¡API£»¡¡while¡¡not¡¡having¡¡to¡¡maintain¡¡an¡¡object¡¡hierarchy¡¡to¡¡watch¡¡which¡¡objects¡¡are¡¡being¡¡referenced¡£¡¡



Finding¡¡a¡¡Room¡¡Grouping¡¡



When¡¡a¡¡number¡¡of¡¡room¡¡groupings¡¡have¡¡been¡¡added£»¡¡you¡¡will¡¡want¡¡to¡¡find¡¡a¡¡room¡¡grouping¡¡¡¡

with¡¡a¡¡particular¡¡description¡£¡¡As¡¡room¡¡groupings¡¡are¡¡a¡¡doubly¡¡linked¡¡list£»¡¡it¡¡means¡¡needing¡¡to¡¡¡¡

iterate¡¡the¡¡list£»¡¡as¡¡follows¡¡£¨added¡¡to¡¡¡¡LightingController£©£º¡¡



¡¡¡¡¡¡¡¡Public¡¡Function¡¡FindRoomGrouping£¨ByVal¡¡description¡¡As¡¡String£©¡¡As¡¡Object¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡curr¡¡As¡¡RoomGrouping¡¡=¡¡_roomGroupings¡£NextItem¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Do¡¡While¡¡curr¡¡IsNot¡¡Nothing¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡curr¡£Description¡£pareTo£¨description£©¡¡=¡¡0¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡curr¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡curr¡¡=¡¡TryCast£¨curr¡£NextItem£»¡¡RoomGrouping£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Loop¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Nothing¡¡

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



¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡iteration¡¡code£»¡¡the¡¡iteration¡¡is¡¡similar¡¡to¡¡the¡¡code¡¡illustrated¡¡earlier¡¡in¡¡the¡¡¡°Storing¡¡a¡¡¡¡

Collection¡¡Using¡¡a¡¡Linked¡¡List¡±¡¡section¡£¡¡The¡¡one¡¡difference¡¡is¡¡that¡¡the¡¡curr¡¡variable¡¡is¡¡of¡¡type¡¡¡¡

RoomGrouping£»¡¡and¡¡because¡¡NextItem¡¡is¡¡of¡¡type¡¡BaseLinkedListItem£»¡¡a¡¡type¡¡cast¡¡is¡¡necessary¡£¡¡¡¡

Then¡¡an¡¡iteration¡¡using¡¡a¡¡While¡¡loop¡¡is¡¡carried¡¡out£»¡¡during¡¡each¡¡iteration£»¡¡a¡¡test¡¡paring¡¡¡¡

curr¡£Description¡¡to¡¡the¡¡parameter¡¡description¡¡is¡¡made¡£¡¡If¡¡an¡¡object¡¡is¡¡found£»¡¡the¡¡handle¡¡to¡¡¡¡

the¡¡RoomGrouping¡¡is¡¡returned£»¡¡and¡¡if¡¡nothing¡¡is¡¡found£»¡¡Nothing¡¡is¡¡returned£»¡¡indicating¡¡that¡¡the¡¡¡¡

RoomGrouping¡¡could¡¡not¡¡be¡¡found¡£¡¡

¡¡¡¡¡¡¡¡¡¡This¡¡method¡¡would¡¡be¡¡used¡¡as¡¡follows£º¡¡



Dim¡¡foundHandle¡¡As¡¡Object¡¡=¡¡controller¡£FindRoomGrouping£¨¡¨description¡¨£©¡¡



¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡has¡¡constructs¡¡that¡¡make¡¡it¡¡possible¡¡to¡¡convert¡¡the¡¡¡¡LightingController¡¡class¡¡¡¡

into¡¡a¡¡class¡¡that¡¡has¡¡array¡¡functionality¡£¡¡The¡¡following¡¡method¡¡in¡¡LightingController¡¡declares¡¡¡¡

array¡­like¡¡functionality£»¡¡which¡¡is¡¡called¡¡a¡¡default¡¡property¡¡



Default¡¡Public¡¡ReadOnly¡¡Property¡¡Item£¨ByVal¡¡description¡¡As¡¡String£©¡¡As¡¡Object¡¡¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡FindRoomGrouping£¨description£©¡¡

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

End¡¡Property¡¡



¡¡¡¡¡¡¡¡¡¡A¡¡Visual¡¡Basic¡¡default¡¡property¡¡is¡¡defined¡¡like¡¡a¡¡property£»¡¡except¡¡that¡¡the¡¡property¡¡is¡¡prefixed¡¡¡¡

with¡¡a¡¡Default¡¡keyword¡£¡¡The¡¡default¡¡property¡¡is¡¡named¡¡¡¡Item¡¡by¡¡convention£»¡¡and¡¡this¡¡name¡¡is¡¡¡¡

used¡¡by¡¡all¡¡¡¡collection¡¡classes¡¡for¡¡consistency¡£¡¡The¡¡implementation¡¡of¡¡a¡¡property¡¡is¡¡just¡¡like¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡238¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

216¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡properties¡£¡¡However£»¡¡the¡¡default¡¡property¡¡is¡¡unique¡¡in¡¡that¡¡you¡¡use¡¡it¡¡like¡¡an¡¡array¡¡refer

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ence¡£¡¡You¡¡could¡¡use¡¡the¡¡default¡¡property¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡foundHandle¡¡As¡¡Object¡¡=¡¡controller£¨¡¨description¡¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡array¡¡index¡¡is¡¡a¡¡string¡¡and¡¡behaves¡¡more¡¡like¡¡a¡¡property¡¡bag¡£¡¡However£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡if¡¡description¡¡were¡¡of¡¡type¡¡¡¡Integer£»¡¡then¡¡the¡¡default¡¡property¡¡would¡¡behave¡¡like¡¡an¡¡index¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Default¡¡properties¡¡are¡¡utility¡­based¡¡and¡¡best¡¡added¡¡to¡¡classes¡¡that¡¡manage¡¡collections¡£¡¡In¡¡the¡¡case¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡LightingController£»¡¡which¡¡manages¡¡a¡¡collection¡¡of¡¡room¡¡groupings£»¡¡use¡¡of¡¡a¡¡default¡¡property¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡appropriate¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡find¡¡a¡¡particular¡¡room¡¡grouping£»¡¡you¡¡would¡¡use¡¡the¡¡methods¡¡or¡¡default¡¡property¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡LightingController¡£¡¡However£»¡¡sometimes¡¡a¡¡user¡¡would¡¡like¡¡to¡¡work¡¡with¡¡all¡¡of¡¡the¡¡room¡¡group

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ings¡¡that¡¡are¡¡available¡¡and¡¡iterate¡¡over¡¡them¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡previous¡¡indexer¡¡example¡¡and¡¡the¡¡¡¡FindRoomGrouping£¨£©¡¡method¡¡returned¡¡an¡¡object¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡iterate¡¡the¡¡room¡¡groupings£»¡¡you¡¡don¡¯t¡¡want¡¡an¡¡object£»¡¡because¡¡you¡¡don¡¯t¡¡know¡¡what¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡object¡¡represents¡£¡¡If¡¡you¡¡call¡¡the¡¡¡¡FindRoomGrouping£¨£©¡¡method¡¡and¡¡you¡¡search¡¡based¡¡on¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡description£»¡¡the¡¡object¡¡that¡¡is¡¡returned¡¡is¡¡cross¡­referenced¡¡with¡¡the¡¡description¡£¡¡However£»¡¡if¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡iterate¡¡using¡¡a¡¡numeric¡¡default¡¡property£»¡¡the¡¡returned¡¡object¡¡means¡¡nothing¡¡to¡¡you¡¡other¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡than¡¡being¡¡associated¡¡with¡¡a¡¡specific¡¡index¡£¡¡What¡¡you¡¡really¡¡want¡¡to¡¡know¡¡is¡¡the¡¡description¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡each¡¡object¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡could¡¡define¡¡a¡¡default¡¡property¡¡with¡¡a¡¡numeric¡¡value£»¡¡as¡¡in¡¡the¡¡following¡¡code£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡iterate¡¡the¡¡individual¡¡elements¡¡to¡¡obtain¡¡descriptions¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Default¡¡Public¡¡ReadOnly¡¡Property¡¡Item£¨ByVal¡¡index¡¡As¡¡Integer£©¡¡As¡¡String¡¡

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

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

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡A¡¡type¡¡can¡¡have¡¡multiple¡¡default¡¡property¡¡definitions£»¡¡but¡¡each¡¡default¡¡property¡¡definition¡¡must¡¡have¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡different¡¡array¡¡parameters¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Suppose¡¡we¡¡implemented¡¡a¡¡numeric¡¡default¡¡property¡£¡¡To¡¡iterate¡¡the¡¡individual¡¡room¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡groupings£»¡¡we¡¡would¡¡need¡¡to¡¡use¡¡the¡¡following¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡c1¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡controller¡£Length¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡description¡¡As¡¡String¡¡=¡¡controller£¨c1£©¡¡

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡239¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡217¡¡



¡¡¡¡¡¡¡¡¡¡This¡¡iteration¡¡code¡¡is¡¡acceptable¡¡and¡¡something¡¡that¡¡we¡¡could¡¡use£»¡¡but¡¡it¡¡involves¡¡adding¡¡¡¡

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¡¡=¡¡_firs

·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨0£© ²È£¨1£©

Äã¿ÉÄÜϲ»¶µÄ