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

µÚ29²¿·Ö

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

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

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




¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡a¡¡basket¡¡of¡¡three¡¡items¡£¡¡These¡¡three¡¡items¡¡can¡¡be¡¡referenced¡¡using¡¡three¡¡indices¡¡£¨0¡¡=¡¡newyork£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡¡=¡¡toronto£»¡¡2¡¡=¡¡losangeles£©¡£¡¡This¡¡solution¡¡creates¡¡a¡¡fixed¡­content¡¡basket¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Alternatively£»¡¡you¡¡could¡¡create¡¡space¡¡in¡¡the¡¡basket¡¡and¡¡then¡¡manually¡¡fill¡¡the¡¡basket£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ReDim¡¡montreal¡£Connections£¨2£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨0£©¡¡=¡¡newyork¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨1£©¡¡=¡¡toronto¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨2£©¡¡=¡¡losangeles¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡ReDim¡¡statement¡¡takes¡¡the¡¡array¡¡referenced¡¡by¡¡the¡¡data¡¡member¡¡Connections¡¡and¡¡real

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡locates¡¡it¡¡to¡¡a¡¡new¡¡size¡£¡¡That¡¡size¡¡could¡¡be¡¡larger¡¡or¡¡smaller¡¡than¡¡the¡¡current¡¡size¡£¡¡In¡¡the¡¡example£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡size¡¡of¡¡the¡¡array¡¡is¡¡2£»¡¡which¡¡does¡¡not¡¡mean¡¡two¡¡elements£»¡¡but¡¡rather¡¡that¡¡it¡¡is¡¡an¡¡array¡¡sized¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡from¡¡the¡¡index¡¡0¡¡to¡¡2¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡ReDim¡¡statement¡¡actually¡¡performs¡¡a¡¡redimension¡¡of¡¡an¡¡array¡£¡¡The¡¡example¡¡demon

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡strates¡¡using¡¡it¡¡on¡¡an¡¡empty¡¡array£»¡¡but¡¡you¡¡can¡¡also¡¡use¡¡it¡¡with¡¡an¡¡array¡¡that¡¡contains¡¡data£»¡¡such¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡to¡¡enlarge¡¡an¡¡array¡¡without¡¡losing¡¡the¡¡old¡¡data¡£¡¡To¡¡keep¡¡the¡¡old¡¡data¡¡in¡¡a¡¡new¡¡array£»¡¡you¡¡use¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡Preserve¡¡keyword£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ReDim¡¡Preserve¡¡montreal¡£Connections£¨2£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨0£©¡¡=¡¡newyork¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨1£©¡¡=¡¡toronto¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections£¨2£©¡¡=¡¡losangeles¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡be¡¡aware¡¡that¡¡if¡¡the¡¡ReDim¡¡statement¡¡causes¡¡an¡¡array¡¡to¡¡shrink£»¡¡you¡¡can¡¡still¡¡lose¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡ªwhatever¡¡elements¡¡are¡¡located¡¡above¡¡the¡¡array¡¡size¡¡are¡¡discarded¡£¡¡To¡¡be¡¡safe£»¡¡before¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡ReDim£»¡¡you¡¡might¡¡want¡¡to¡¡reference¡¡the¡¡array¡¯s¡¡¡¡Length¡¡property¡¡to¡¡verify¡¡that¡¡you¡¡are¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡shrinking¡¡the¡¡array¡¡and¡¡losing¡¡data¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡115¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡93¡¡



¡¡¡¡¡¡¡¡¡¡Using¡¡either¡¡the¡¡fixed¡­content¡¡or¡¡ReDim¡¡approach£»¡¡you¡¡allocate¡¡space¡¡for¡¡the¡¡array¡¡and¡¡assign¡¡¡¡

the¡¡variables¡¡representing¡¡the¡¡cities¡¡to¡¡the¡¡individual¡¡elements¡¡of¡¡the¡¡array¡£¡¡Since¡¡Connections¡¡¡¡

is¡¡an¡¡array¡¡of¡¡value¡¡types£»¡¡the¡¡connections¡¡within¡¡the¡¡connections¡¡are¡¡not¡¡set£»¡¡as¡¡shown¡¡in¡¡¡¡

Figure¡¡4¡­10¡£¡¡



Figure¡¡4¡­10¡£¡¡¡¡The¡¡problem¡¡of¡¡the¡¡missing¡¡connections¡¡for¡¡New¡¡York¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡is¡¡that¡¡the¡¡Connections¡¡array¡¡for¡¡New¡¡York¡¡is¡¡missing¡£¡¡Of¡¡course£»¡¡you¡¡could¡¡be¡¡¡¡

logical¡¡and¡¡say¡¡it¡¡is¡¡missing¡¡because¡¡the¡¡Connections¡¡data¡¡member¡¡for¡¡New¡¡York¡¡has¡¡not¡¡yet¡¡¡¡

been¡¡defined¡£¡¡But£»¡¡and¡¡it¡¡is¡¡a¡¡big¡¡but£»¡¡think¡¡about¡¡how¡¡data¡¡is¡¡referenced¡¡and¡¡think¡¡about¡¡the¡¡¡¡

behavior¡¡summarized¡¡in¡¡Table¡¡4¡­1¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Node¡¡is¡¡a¡¡value¡¡type£»¡¡and¡¡when¡¡a¡¡value¡¡type¡¡is¡¡assigned£»¡¡the¡¡values¡¡within¡¡the¡¡type¡¡are¡¡copied¡£¡¡¡¡

Because¡¡the¡¡connections¡¡for¡¡New¡¡York¡¡have¡¡not¡¡been¡¡assigned£»¡¡the¡¡Montreal¡¡Connections¡¡array¡¡¡¡

will¡¡not¡¡contain¡¡any¡¡connections¡¡from¡¡New¡¡York¡£¡¡And¡¡if¡¡you¡¡modify¡¡the¡¡original¡¡variable¡¡for¡¡¡¡

New¡¡York¡¡and¡¡its¡¡connections£»¡¡those¡¡changes¡¡will¡¡not¡¡be¡¡reflected¡¡in¡¡the¡¡array¡¡of¡¡connections¡¡¡¡

that¡¡Montreal¡¡has¡£¡¡

¡¡¡¡¡¡¡¡¡¡At¡¡this¡¡point£»¡¡you¡¡might¡¡think¡¡this¡¡is¡¡not¡¡a¡¡problem£»¡¡but¡¡consider¡¡the¡¡following¡¡New¡¡York¡¡code£º¡¡



ReDim¡¡newyork¡£Connections£¨2£©¡¡¡¡

newyork¡£Connections£¨0£©¡¡=¡¡montreal¡¡

newyork¡£Connections£¨1£©¡¡=¡¡houston¡¡

newyork¡£Connections£¨2£©¡¡=¡¡miami¡¡



¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡example£»¡¡New¡¡York¡¡has¡¡a¡¡connection¡¡to¡¡Montreal£»¡¡and¡¡Montreal¡¡has¡¡a¡¡connection¡¡¡¡

to¡¡New¡¡York£»¡¡pleting¡¡a¡¡full¡¡circle¡£¡¡muters¡¡would¡¡want¡¡this¡¡ability¡¡to¡¡fly¡¡back¡¡and¡¡forth¡¡¡¡

between¡¡cities¡£¡¡But¡¡because¡¡we¡¡are¡¡using¡¡value¡¡types£»¡¡it¡¡is¡¡not¡¡possible¡¡to¡¡fly¡¡back¡¡and¡¡forth£»¡¡as¡¡¡¡

illustrated¡¡by¡¡Figure¡¡4¡­11¡£¡¡

¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡­11¡¡illustrates¡¡that¡¡recursion¡¡with¡¡value¡¡types¡¡does¡¡not¡¡work¡£¡¡It¡¡shows¡¡that¡¡there¡¡¡¡

are¡¡connections¡¡from¡¡New¡¡York¡¡to¡¡Montreal¡£¡¡But¡¡following¡¡the¡¡connection¡¡to¡¡Montreal£»¡¡it¡¡would¡¡¡¡

appear¡¡that¡¡New¡¡York¡¡has¡¡no¡¡connections£»¡¡which¡¡is¡¡blatantly¡¡false£»¡¡because¡¡we¡¡can¡¡see¡¡the¡¡¡¡

connection¡¡from¡¡New¡¡York¡¡to¡¡Montreal¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡116¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

94¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡­11¡£¡¡Missing¡¡connections¡¡for¡¡New¡¡York¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡value¡¡types¡¡are¡¡assigned£»¡¡you¡¡are¡¡copying¡¡contents¡¡of¡¡the¡¡value¡¡type¡¡and¡¡thus¡¡getting¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡snapshot¡¡of¡¡the¡¡state¡¡of¡¡an¡¡object¡¡at¡¡some¡¡period¡¡in¡¡time¡£¡¡In¡¡essence£»¡¡the¡¡code¡¡illustrates¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡chicken¡­and¡­egg¡¡problem¡¡of¡¡defining¡¡the¡¡connections¡¡for¡¡a¡¡particular¡¡city¡¡and¡¡then¡¡assigning¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡them¡£¡¡For¡¡value¡¡types£»¡¡how¡¡can¡¡you¡¡assign¡¡the¡¡connection¡¡of¡¡one¡¡city¡¡to¡¡another¡¡when¡¡the¡¡to

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡­assigned¡¡connection¡¡does¡¡not¡¡exist£¿¡¡The¡¡short¡¡answer¡¡is¡¡you¡¡can¡¯t¡£¡¡The¡¡long¡¡answer¡¡is¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can£»¡¡but¡¡it¡¡would¡¡mean¡¡executing¡¡an¡¡infinite¡¡loop£»¡¡which¡¡is¡¡of¡¡no¡¡use¡¡to¡¡us£»¡¡because¡¡we¡¡want¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡do¡¡something¡¡with¡¡the¡¡data¡¡once¡¡it¡¡has¡¡been¡¡assigned¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Switching¡¡to¡¡a¡¡Class¡¡to¡¡Define¡¡a¡¡Node¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡fix¡¡the¡¡chicken¡­and¡­egg¡¡problem£»¡¡we¡¡need¡¡to¡¡use¡¡reference¡¡types¡¡instead¡¡of¡¡value¡¡types¡£¡¡This¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡means¡¡we¡¡need¡¡to¡¡change¡¡the¡¡declaration¡¡of¡¡Node¡¡from¡¡a¡¡¡¡Structure¡¡to¡¡a¡¡Class£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡Node¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡RootNodes¡¡As¡¡Node£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡117¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡95¡¡



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡city¡¡As¡¡String£»¡¡ByVal¡¡X¡¡As¡¡Double£»¡¡ByVal¡¡Y¡¡As¡¡Double£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£CityName¡¡=¡¡city¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£X¡¡=¡¡X¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Y¡¡=¡¡Y¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Connections¡¡=¡¡Nothing¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡change¡¡is¡¡a¡¡one¡­liner¡£¡¡After¡¡the¡¡switch£»¡¡if¡¡we¡¡executed¡¡the¡¡same¡¡assignment¡¡code¡¡as¡¡in¡¡¡¡

the¡¡previous¡¡section¡¡when¡¡Node¡¡was¡¡a¡¡value¡¡type£»¡¡the¡¡data¡¡structure¡¡shown¡¡in¡¡Figure¡¡4¡­12¡¡would¡¡¡¡

be¡¡created¡£¡¡



Figure¡¡4¡­12¡£¡¡A¡¡valid¡¡state¡¡for¡¡the¡¡New¡¡York¡¡Node¡¡instance¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Looking¡¡at¡¡the¡¡node¡¡structure¡¡in¡¡Figure¡¡4¡­12£»¡¡you¡¡can¡¡see¡¡that¡¡New¡¡York¡¡points¡¡to¡¡Montreal¡¡¡¡

and¡¡back¡¡again¡£¡¡The¡¡infinite¡¡connection¡¡does¡¡not¡¡mean¡¡that¡¡you¡¡are¡¡using¡¡infinite¡¡resources¡£¡¡¡¡

Instead£»¡¡it¡¡means¡¡one¡¡reference¡¡is¡¡being¡¡set¡¡to¡¡another£»¡¡as¡¡illustrated¡¡in¡¡Figure¡¡4¡­13¡£¡¡



¡¡¡¡¡¡¡¡Variable¡¡montreal¡¡points¡¡to¡¡heap¡¡

¡¡¡¡¡¡¡¡memory¡¡that¡¡contains¡¡the¡¡data¡¡for¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡There¡¡is¡¡a¡¡reference¡¡in¡¡newyork¡¡to¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡city¡¡Montreal¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡and¡¡vice¡¡versa¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡newyork¡¡



¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡

¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡

¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡

¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡newyork¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Array¡¡element¡¡is¡¡a¡¡reference¡¡to¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡other¡¡heap¡¡memory¡¡



Figure¡¡4¡­13¡£¡¡Recursive¡¡assignment¡¡that¡¡seems¡¡like¡¡infinite¡¡resources¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡118¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

96¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡apparent¡¡infinite¡¡resources¡¡are¡¡the¡¡cross¡­reference¡¡recursive¡¡assignment¡¡of¡¡two¡¡pieces¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡heap¡¡memory¡£¡¡It¡¡is¡¡fine¡¡to¡¡do¡¡this£»¡¡and¡¡this¡¡ability¡¡is¡¡one¡¡of¡¡the¡¡reasons¡¡why¡¡people¡¡prefer¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡reference¡¡types¡¡to¡¡value¡¡types¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Static¡¡Data¡¡Members¡¡and¡¡Methods¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¯ve¡¡seen¡¡how¡¡a¡¡constructor¡¡can¡¡be¡¡used¡¡to¡¡initialize¡¡the¡¡state¡¡of¡¡a¡¡particular¡¡instance¡¡of¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡£¡¡Now¡¡we¡¡need¡¡to¡¡define¡¡a¡¡constructor¡¡for¡¡the¡¡tree¡¡structure¡¡shown¡¡in¡¡Figure¡¡4¡­2¡£¡¡A¡¡tree¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implies¡¡a¡¡starting¡¡point£»¡¡but¡¡the¡¡code¡¡for¡¡the¡¡flight¡¡connections¡¡does¡¡not¡¡imply¡¡a¡¡single¡¡starting¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡point¡£¡¡Instead£»¡¡we¡¡have¡¡the¡¡declaration¡¡of¡¡a¡¡number¡¡of¡¡variables¡¡where¡¡the¡¡identifier¡¡of¡¡each¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡is¡¡a¡¡city¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡with¡¡such¡¡a¡¡declaration¡¡is¡¡that£»¡¡if¡¡you¡¡want¡¡to¡¡navigate¡¡a¡¡tree¡¡structure£»¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡need¡¡to¡¡know¡¡the¡¡individual¡¡names¡¡of¡¡the¡¡variables¡¡and¡¡navigate¡¡the¡¡tree¡¡structure¡¡of¡¡each¡¡vari

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡able¡£¡¡This¡¡is¡¡not¡¡a¡¡plausible¡¡solution¡£¡¡You¡¡want¡¡to¡¡create¡¡a¡¡single¡¡overall¡¡point¡¡from¡¡where¡¡all¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡cities¡¡can¡¡be¡¡referenced¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡solution¡¡lies¡¡in¡¡using¡¡an¡¡array¡¡like¡¡that¡¡used¡¡for¡¡the¡¡data¡¡member¡¡Connections¡£¡¡To¡¡address¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡problem¡¡of¡¡providing¡¡a¡¡single¡¡access¡¡point£»¡¡we¡¡declare¡¡a¡¡shared¡¡data¡¡member£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡Node¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡RootNodes¡¡As¡¡Node£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨string¡¡city£»¡¡double¡¡x£»¡¡double¡¡y£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£CityName¡¡=¡¡city¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£X¡¡=¡¡x¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Y¡¡=¡¡y¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Connections¡¡=¡¡Nothing¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bold¡¡code¡¡shows¡¡the¡¡declaration¡¡of¡¡a¡¡data¡¡member¡¡with¡¡the¡¡modifier¡¡Shared¡¡to¡¡indi

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cate¡¡that¡¡the¡¡data¡¡member¡¡is¡¡shared¡£¡¡So£»¡¡just¡¡what¡¡does¡¡shared¡¡mean¡¡in¡¡this¡¡context£¿¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡that¡¡your¡¡family¡¡consists¡¡of¡¡your¡¡significant¡¡other¡¡and¡¡two¡¡kids¡£¡¡One¡¡day£»¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡wander¡¡into¡¡the¡¡cell¡¡phone¡¡store¡¡and¡¡decide¡¡to¡¡buy¡¡four¡¡identical¡¡cell¡¡phones£»¡¡as¡¡the¡¡store¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡having¡¡a¡¡family¡¡deal¡£¡¡When¡¡each¡¡phone¡¡is¡¡

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

Äã¿ÉÄÜϲ»¶µÄ