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

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡that¡¡contains¡¡a¡¡valid¡¡

¡¡Dim¡¡var¡¡As¡¡MyValueType¡¡=¡¡New¡¡MyValueType£¨£©¡¡

¡¡Dim¡¡copiedVar¡¡As¡¡MyValueType¡¡=¡¡var¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instance¡¡£¨e¡£g¡££»¡¡var£©¡¡



¡¡Console¡£WriteLine£¨¡¨var¡¡value=¡¨¡¡&¡¡var¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¨¡¡copiedVar¡¡value=¡¨¡¡&¡¡copiedVar¡£value£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡an¡¡instance¡¡is¡¡allocated£»¡¡the¡¡data¡¡

¡¡var¡£value¡¡=¡¡10¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡members¡¡are¡¡assigned¡¡their¡¡appropriate¡¡

¡¡Console¡£WriteLine£¨¡¨var¡¡value=¡¨¡¡&¡¡var¡£value¡¡&¡¡_¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡default¡¡value£»¡¡which¡¡is¡¡0¡¡for¡¡Integer¡¡and¡¡most¡¡

¡¡¡¡¡¡¡¡¡¡¡¨¡¡copiedVar¡¡value=¡¨¡¡&¡¡copiedVar¡£value£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡numbers¡£¡¡For¡¡strings£»¡¡it¡¡is¡¡an¡¡empty¡¡buffer¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡These¡¡lines¡¡display¡¡the¡¡empty¡¡values¡£¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡value¡¡of¡¡var¡£value¡¡is¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡modified¡¡from¡¡0¡¡to¡¡10¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡contents¡¡of¡¡var¡¡and¡¡copiedVar¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡displayed¡¡again¡¡to¡¡show¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡variable¡¡has¡¡been¡¡modified¡¡



Figure¡¡4¡­5¡£¡¡¡¡Using¡¡a¡¡custom¡¡value¡¡type¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡As¡¡a¡¡parison£»¡¡the¡¡same¡¡operations¡¡can¡¡be¡¡carried¡¡out¡¡using¡¡a¡¡reference¡¡type£»¡¡where¡¡the¡¡¡¡

reference¡¡code¡¡is¡¡as¡¡follows£º¡¡



Dim¡¡val¡¡As¡¡MyReferenceType¡¡=¡¡New¡¡MyReferenceType£¨£©¡¡

Dim¡¡copiedVal¡¡As¡¡MyReferenceType¡¡=¡¡val¡¡



Console¡£WriteLine£¨¡¨val¡¡value=¡¨¡¡&¡¡val¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¨¡¡copiedVal¡¡value=¡¨¡¡&¡¡copiedVal¡£value£©¡¡

val¡£value¡¡=¡¡10¡¡

Console¡£WriteLine£¨¡¨val¡¡value=¡¨¡¡&¡¡val¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¨¡¡copiedVal¡¡value=¡¨¡¡&¡¡copiedVal¡£value£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡So£»¡¡if¡¡two¡¡code¡¡pieces¡¡are¡¡functionally¡¡identical£»¡¡with¡¡the¡¡single¡¡difference¡¡being¡¡their¡¡type¡¡¡¡

£¨value¡¡type¡¡versus¡¡reference¡¡type£©£»¡¡will¡¡they¡¡generate¡¡the¡¡same¡¡results£¿¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Running¡¡both¡¡code¡¡pieces¡¡results¡¡in¡¡the¡¡following¡¡output£º¡¡



var¡¡value=0¡¡copiedVar¡¡value=0¡¡

var¡¡value=10¡¡copiedVar¡¡value=0¡¡

val¡¡value=0¡¡copiedVal¡¡value=0¡¡

val¡¡value=10¡¡copiedVal¡¡value=10¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Looking¡¡closely¡¡at¡¡the¡¡results£»¡¡you¡¡see¡¡that¡¡two¡¡functionally¡¡identical¡¡code¡¡pieces¡¡that¡¡differ¡¡¡¡

only¡¡in¡¡the¡¡variable¡¡type¡¡they¡¡use¡¡generate¡¡pletely¡¡different¡¡results£º¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡106¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡assign¡¡and¡¡modify¡¡a¡¡value¡¡type£»¡¡only¡¡the¡¡contents¡¡of¡¡the¡¡modified¡¡variable¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡change¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡assign¡¡and¡¡modify¡¡a¡¡reference¡¡type£»¡¡the¡¡original¡¡object¡¡changes£»¡¡so¡¡the¡¡original¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡and¡¡assigned¡¡variable¡¡change¡¡because¡¡they¡¡both¡¡point¡¡to¡¡the¡¡same¡¡original¡¡object¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡example¡¡demonstrates¡¡that¡¡when¡¡defining¡¡user¡­defined¡¡types£»¡¡you¡¡need¡¡to¡¡be¡¡careful¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡how¡¡you¡¡treat¡¡value¡¡and¡¡reference¡¡types¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡learned¡¡in¡¡Chapter¡¡2£»¡¡a¡¡value¡¡type¡¡is¡¡stored¡¡on¡¡the¡¡stack¡£¡¡Thus£»¡¡declaring¡¡a¡¡user

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡defined¡¡value¡¡type¡¡means¡¡the¡¡full¡¡contents¡¡of¡¡the¡¡user¡­defined¡¡type¡¡are¡¡stored¡¡on¡¡the¡¡stack£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡when¡¡you¡¡assign¡¡one¡¡value¡¡type¡¡variable¡¡to¡¡another¡¡value¡¡type¡¡variable£»¡¡you¡¡are¡¡copying¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡plete¡¡contents¡¡of¡¡the¡¡value¡¡type¡£¡¡This¡¡copying¡¡was¡¡obvious¡¡in¡¡our¡¡example¡¡when¡¡we¡¡used¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡simple¡¡numeric¡¡number¡¡types¡¡£¨such¡¡as¡¡Double£©£»¡¡but¡¡when¡¡you¡¡copy¡¡plete¡¡structures¡¡with¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contents£»¡¡the¡¡side¡¡effects¡¡might¡¡not¡¡be¡¡what¡¡you¡¡expect¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Value¡¡Types¡¡That¡¡Contain¡¡Reference¡¡Types¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Value¡¡types¡¡copy¡¡the¡¡contents¡¡when¡¡you¡¡assign¡¡variables£»¡¡but¡¡there¡¡is¡¡caveat¡¡in¡¡that¡¡this¡¡rule¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡does¡¡not¡¡apply¡¡if¡¡the¡¡value¡¡type¡¡contains¡¡a¡¡reference¡¡type¡£¡¡Consider¡¡the¡¡following¡¡declaration¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Structure¡¡MyValueTypeWithReferenceType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡reference¡¡As¡¡MyReferenceType¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Structure¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡line¡¡in¡¡the¡¡value¡¡type¡¡declaration¡¡contains¡¡a¡¡single¡¡value¡¡type¡¡£¨Integer£©¡£¡¡The¡¡second¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡line¡¡is¡¡the¡¡data¡¡member¡¡declaration¡¡that¡¡is¡¡a¡¡reference¡¡type¡£¡¡The¡¡declaration¡¡implies¡¡that¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡type¡¡is¡¡stored¡¡on¡¡the¡¡stack£»¡¡but¡¡the¡¡reference¡¡type¡¡is¡¡on¡¡the¡¡heap¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡value¡¡type¡¡that¡¡contains¡¡a¡¡reference¡¡type¡¡is¡¡manipulated¡¡using¡¡the¡¡following¡¡test¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡var¡¡As¡¡MyValueTypeWithReferenceType¡¡=¡¡New¡¡MyValueTypeWithReferenceType£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡var¡£reference¡¡=¡¡New¡¡MyReferenceType£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡copiedVar¡¡As¡¡MyValueTypeWithReferenceType¡¡=¡¡var¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨var¡¡value=¡¨¡¡&¡¡var¡£reference¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨¡¡copiedVar¡¡value=¡¨¡¡&¡¡copiedVar¡£reference¡£value£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡var¡£reference¡£value¡¡=¡¡10¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨var¡¡value=¡¨¡¡&¡¡var¡£reference¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨¡¡copiedVar¡¡value=¡¨¡¡&¡¡copiedVar¡£reference¡£value£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡It¡¡is¡¡important¡¡to¡¡realize¡¡that¡¡allocating¡¡MyValueTypeWithReferenceType¡¡does¡¡not¡¡imply¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡an¡¡allocation¡¡of¡¡the¡¡embedded¡¡custom¡¡type¡£¡¡In¡¡the¡¡test¡¡code£»¡¡the¡¡allocation¡¡of¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyValueTypeWithReferenceType¡¡is¡¡the¡¡same¡¡as¡¡the¡¡previous¡¡code¡¡examples£»¡¡but¡¡a¡¡second¡¡allocation¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡MyReferenceType¡¡is¡¡required¡¡because¡¡MyReferenceType¡¡is¡¡a¡¡reference¡¡type¡£¡¡Had¡¡MyReferenceType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡been¡¡a¡¡value¡¡type£»¡¡the¡¡allocation¡¡would¡¡not¡¡have¡¡been¡¡necessary¡£¡¡But¡¡if¡¡you¡¡allocate¡¡a¡¡value¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡like¡¡a¡¡reference¡¡type£»¡¡the¡¡piler¡¡will¡¡ignore¡¡the¡¡directive¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡107¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡Running¡¡the¡¡test¡¡code¡¡results¡¡in¡¡the¡¡following£º¡¡



var¡¡value=0¡¡copiedVar¡¡value=0¡¡

var¡¡value=10¡¡copiedVar¡¡value=10¡¡



¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡assign¡¡and¡¡modify¡¡an¡¡embedded¡¡reference¡¡type£»¡¡the¡¡reference¡¡type¡¡instance¡¡is¡¡¡¡

modified¡¡for¡¡both¡¡variables¡£¡¡In¡¡this¡¡case£»¡¡when¡¡we¡¡assigned¡¡the¡¡value¡¡type£»¡¡the¡¡contents¡¡were¡¡¡¡

copied£»¡¡including¡¡the¡¡pointer¡¡to¡¡the¡¡reference¡¡type¡£¡¡

¡¡¡¡¡¡¡¡¡¡Table¡¡4¡­1¡¡summarizes¡¡the¡¡behavior¡¡of¡¡types¡¡when¡¡the¡¡allocated¡¡variable¡¡is¡¡assigned¡¡to¡¡¡¡

another¡¡variable¡¡and¡¡the¡¡data¡¡member¡¡from¡¡the¡¡original¡¡is¡¡modified¡£¡¡For¡¡example£»¡¡if¡¡the¡¡code¡¡¡¡

is¡¡custom2¡¡=¡¡custom1¡¡custom1¡£member¡¡=¡¡£§new¡¡value£§£»¡¡what¡¯s¡¡the¡¡value¡¡of¡¡custom2¡£member£¿¡¡¡¡



Table¡¡4¡­1¡£¡¡Behavior¡¡When¡¡the¡¡Allocated¡¡Variable¡¡Is¡¡Assigned¡¡to¡¡Another¡¡Variable¡¡and¡¡the¡¡Data¡¡¡¡

Member¡¡from¡¡the¡¡Original¡¡Is¡¡Modified¡¡



Type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Behavior¡¡



Value¡¡type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡data¡¡member¡¡is¡¡not¡¡modified¡¡



Reference¡¡type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡data¡¡member¡¡is¡¡modified¡¡



Value¡¡type¡¡embedding¡¡value¡¡type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡embedded¡¡data¡¡member¡¡is¡¡not¡¡modified¡¡



Value¡¡type¡¡embedding¡¡reference¡¡type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡embedded¡¡data¡¡member¡¡is¡¡modified¡¡



Reference¡¡type¡¡embedding¡¡value¡¡type¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡embedded¡¡data¡¡member¡¡is¡¡modified¡¡



Reference¡¡type¡¡embedding¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigned¡¡embedded¡¡data¡¡member¡¡is¡¡modified¡¡

reference¡¡type¡¡



Value¡¡Types¡¡and¡¡Parameters¡¡



Another¡¡value¡¡type¡¡constraint¡¡relates¡¡to¡¡how¡¡variables¡¡are¡¡stored¡¡and¡¡manipulated¡¡when¡¡they¡¡¡¡

are¡¡passed¡¡via¡¡a¡¡method¡£¡¡Suppose¡¡you¡¡create¡¡a¡¡method¡¡that¡¡has¡¡as¡¡parameters¡¡a¡¡value¡¡type¡¡and¡¡¡¡

a¡¡reference¡¡type¡£¡¡If¡¡in¡¡the¡¡method¡¡the¡¡parameters¡¡are¡¡modified£»¡¡what¡¡modifications¡¡does¡¡the¡¡¡¡

caller¡¡of¡¡the¡¡method¡¡see£¿¡¡Consider¡¡the¡¡following¡¡code£º¡¡



Sub¡¡Method£¨ByVal¡¡value¡¡As¡¡MyValueType£»¡¡ByVal¡¡reference¡¡As¡¡MyReferenceType£©¡¡

¡¡¡¡value¡£value¡¡=¡¡10¡¡

¡¡¡¡reference¡£value¡¡=¡¡10¡¡

End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡caller¡¡can¡¡pass¡¡in¡¡instances¡¡of¡¡a¡¡value¡¡type¡¡and¡¡a¡¡reference¡¡type¡¡that¡¡are¡¡manipulated¡¡¡¡

in¡¡the¡¡context¡¡of¡¡the¡¡method¡£¡¡Now¡¡let¡¯s¡¡call¡¡Method£¨£©¡¡with¡¡the¡¡following¡¡code£º¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡108¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡MyValueType¡¡=¡¡New¡¡MyValueType£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡reference¡¡As¡¡MyReferenceType¡¡=¡¡New¡¡MyReferenceType£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Method£¨value£»¡¡reference£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨value¡¡value=¡¨¡¡&¡¡value¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨¡¡reference¡¡value=¡¨¡¡&¡¡reference¡£value£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡calling¡¡code¡¡instantiates¡¡the¡¡types¡¡MyValueType¡¡and¡¡MyReferenceType£»¡¡calls¡¡the¡¡method¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Method£¨£©£»¡¡and¡¡then¡¡inspects¡¡the¡¡value¡¡of¡¡the¡¡data¡¡member¡¡value¡¡from¡¡the¡¡value¡¡and¡¡reference¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Running¡¡the¡¡code¡¡will¡¡generate¡¡output¡¡similar¡¡to¡¡the¡¡following£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡value=0¡¡reference¡¡value=10¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡executed¡¡code¡¡shows¡¡that¡¡the¡¡value¡¡type¡¡£¨MyValueType£©¡¡data¡¡member¡¡was¡¡not¡¡altered£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whereas¡¡the¡¡reference¡¡type¡¡£¨MyReferenceType£©¡¡data¡¡member¡¡was¡¡altered¡£¡¡This¡¡is¡¡correct£»¡¡and¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡demonstrates¡¡that¡¡when¡¡you¡¡call¡¡a¡¡method£»¡¡you¡¡are¡¡assigning¡¡the¡¡parameters¡¡of¡¡the¡¡method¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variables¡¡in¡¡the¡¡called¡¡method¡£¡¡Looking¡¡back¡¡at¡¡Table¡¡4¡­1£»¡¡you¡¡can¡¡see¡¡that¡¡when¡¡you¡¡assign¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡to¡¡a¡¡value¡¡type£»¡¡manipulating¡¡the¡¡assigned¡¡instance¡¡does¡¡not¡¡change¡¡the¡¡original¡¡instance¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡you¡¡can¡¡gain¡¡some¡¡control¡¡over¡¡how¡¡parameters¡¡are¡¡handled¡£¡¡When¡¡Method£¨£©¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡declared£»¡¡the¡¡assumption¡¡is¡¡made¡¡that¡¡all¡¡of¡¡the¡¡parameters¡¡are¡¡passed¡¡to¡¡the¡¡method¡¡imple

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mentation¡¡as¡¡value¡¡types¡£¡¡It¡¡is¡¡possible¡¡to¡¡pass¡¡the¡¡parameters¡¡to¡¡the¡¡method¡¡as¡¡reference¡¡types£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡determined¡¡by¡¡one¡¡of¡¡the¡¡following¡¡keywords£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal£º¡¡When¡¡a¡¡method¡¡has¡¡parameters¡¡declared¡¡with¡¡the¡¡ByVal¡¡keyword£»¡¡the¡¡parameter¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡has¡¡its¡¡value¡¡copied¡¡from¡¡the¡¡caller¡¡variable¡¡to¡¡method¡¡implementation¡¡variable£»¡¡even¡¡if¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡a¡¡reference¡¡type¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByRef£º¡¡When¡¡a¡¡method¡¡has¡¡parameters¡¡declared¡¡with¡¡the¡¡ByRef¡¡keyword£»¡¡the¡¡caller¡¡variable¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡not¡¡copied¡¡to¡¡the¡¡parameter¡¡variable£»¡¡rather£»¡¡the¡¡variable¡¡used¡¡by¡¡the¡¡caller¡¡and¡¡the¡¡method¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡are¡¡one¡¡and¡¡the¡¡same¡£¡¡Thus£»¡¡changes¡¡to¡¡the¡¡method¡¡variable£»¡¡as¡¡in¡¡the¡¡previous¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡example£»¡¡would¡¡be¡¡visible¡¡to¡¡the¡¡caller¡¡of¡¡the¡¡method¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡­6¡¡illustrates¡¡how¡¡to¡¡use¡¡the¡¡ByRef¡¡keyword¡¡so¡¡that¡¡a¡¡modified¡¡value¡¡type¡¡will¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡visible¡¡to¡¡the¡¡caller¡¡of¡¡the¡¡method¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡you¡¡have¡¡an¡¡understanding¡¡of¡¡the¡¡depth¡­first¡¡search¡¡algorithm¡¡and¡¡how¡¡the¡¡data¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡structure¡¡will¡¡be¡¡defined¡¡as¡¡a¡¡user¡­defined¡¡value¡¡type£»¡¡let¡¯s¡¡get¡¡started¡¡building¡¡the¡¡search¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡algorithm¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡109¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByRef¡¡is¡¡associated¡¡with¡¡the¡¡parameter¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡indicating¡¡that¡¡the¡¡value¡¡variable¡¡can¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡modified¡¡in¡¡the¡¡method¡¡and¡¡the¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡result¡¡will¡¡be¡¡visible¡¡to¡¡the¡¡caller¡¡



¡¡Sub¡¡Method£¨ByRef¡¡value¡¡As¡¡MyValueType£»¡¡ByVal¡¡reference¡¡As¡¡MyReferenceType£©¡¡

¡¡¡¡¡¡¡¡¡¡value¡£value¡¡=¡¡10¡¡

¡¡¡¡¡¡¡¡¡¡reference¡£value¡¡=¡¡10¡¡

¡¡End¡¡Sub¡¡



¡¡Dim¡¡value¡¡As¡¡MyValueType¡¡

¡¡Dim¡¡reference¡¡As¡¡MyReferenceType¡¡=¡¡New¡¡MyReferenceType£¨£©¡¡

¡¡Method£¨value£»¡¡reference£©¡¡

¡¡Console¡£WriteLine£¨¡¨value¡¡value=¡¨¡¡&¡¡value¡£value¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¨¡¡reference¡¡value=¡¨¡¡&¡¡reference¡£value£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡variable¡¡value¡£value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡will¡¡contain¡¡the¡¡value¡¡of¡¡10¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡assigned¡¡in¡¡the¡¡method¡¡



Figure¡¡4¡­6¡£¡¡¡¡Using¡¡the¡¡ByRef¡¡keyword¡¡



Organizing¡¡the¡¡Search¡¡Algorithm¡¡



The¡¡search¡¡algorithm¡¡we¡¡will¡¡write¡¡in¡¡this¡¡chapter¡¡deals¡¡with¡¡the¡¡problem¡¡of¡¡planning¡¡a¡¡flight¡¡¡¡

from¡¡point¡¡A¡¡to¡¡point¡¡B¡£¡¡The¡¡first¡¡step¡¡is¡¡to¡¡figure¡¡out¡¡the¡¡features¡¡we¡¡need¡¡to¡¡implement¡£¡¡Here¡¯s¡¡¡¡

a¡¡summary£º¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡data¡¡structure¡¡implements¡¡the¡¡node¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡node¡¡can¡¡contain¡¡references¡¡to¡¡other¡¡nodes¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Each¡¡node¡¡has¡¡a¡¡description¡¡and¡¡unique¡¡identifier¡¡to¡¡distinguish¡¡it¡¡from¡¡other¡¡nodes¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡All¡¡of¡¡the¡¡nodes¡¡have¡¡flight¡¡information¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡An¡¡algorithm¡¡will¡¡traverse¡¡the¡¡nodes¡¡and¡¡keep¡¡track¡¡of¡¡its¡¡path¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡path¡¡is¡¡returned¡¡as¡¡a¡¡list¡¡of¡¡nodes¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡structure¡¡is¡¡based¡¡on¡¡the¡¡problem¡¡of¡¡planning¡¡a¡¡flight¡¡between¡¡two¡¡places£»¡¡as¡¡¡¡

illustrated¡¡in¡¡Figure¡¡4¡­7¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡110¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

88¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡­7¡£¡¡Planning¡¡flight¡¡routes¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡shown¡¡in¡¡Figure¡¡4¡­7£»¡¡three¡¡main¡¡attributes¡¡describe¡¡an¡¡individual¡¡node¡¡in¡¡the¡¡flight¡¡route£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡City¡¡name£º¡¡A¡¡description¡¡that¡¡will¡¡be¡¡used¡¡as¡¡a¡¡key¡¡when¡¡a¡¡user¡¡defines¡¡a¡¡starting¡¡and¡¡end¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡point¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Coordinates£º¡¡An¡¡illustrative¡¡approach¡¡used¡¡to¡¡describe¡¡how¡¡cities¡¡are¡¡located¡¡in¡¡relation¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡each¡¡other¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Connections£º¡¡A¡¡representative¡¡connection¡¡between¡¡two¡¡cities¡£¡¡As¡¡in¡¡real¡¡life£»¡¡not¡¡all¡¡cities¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connect¡¡to¡¡other¡¡cities¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡the¡¡scope¡¡of¡¡this¡¡chapter£»¡¡there¡¡are¡¡only¡¡two¡¡projects£º¡¡a¡¡class¡¡library¡¡that¡¡contains¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡depth¡­first¡¡search¡¡algorithm¡¡and¡¡the¡¡testing¡¡application¡£¡¡The¡¡project¡¡structure¡¡looks¡¡like¡¡Figure¡¡4¡­8¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡with¡¡the¡¡examples¡¡in¡¡the¡¡previous¡¡chapters£»¡¡remember¡¡to¡¡add¡¡a¡¡reference¡¡to¡¡the¡¡class¡¡library¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨SearchSolution£©¡¡and¡¡to¡¡set¡¡the¡¡test¡¡project¡¡£¨TestSearchSolution£©¡¡as¡¡the¡¡startup¡¡project¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡­8¡£¡¡Solution¡¡project¡¡structure¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡111¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



Writing¡¡the¡¡Depth¡­First¡¡Search¡¡Code¡¡



We¡¡will¡¡implement¡¡the¡¡depth¡­first¡¡search¡¡algorithm¡¡in¡¡three¡¡main¡¡steps¡£¡¡The¡¡first¡¡step¡¡is¡¡to¡¡¡¡

define¡¡and¡¡implement¡¡the¡¡data¡¡structure¡£¡¡The¡¡second¡¡step¡¡is¡¡to¡¡implement¡¡the¡¡algorithm¡¡and¡¡¡¡

tests¡£¡¡Finally£»¡¡we¡¯ll¡¡run¡¡the¡¡algorithm¡¡and¡¡see¡¡what¡¡route¡¡has¡¡been¡¡found¡£¡¡



Defining¡¡and¡¡Implementing¡¡the¡¡Data¡¡Structure¡¡



As¡¡I¡¡mentioned¡¡earlier£»¡¡for¡¡the¡¡most¡¡part£»¡¡developers¡¡use¡¡the¡¡Class¡¡keyword¡¡to¡¡define¡¡a¡¡data¡¡¡¡

structure¡¡as¡¡a¡¡reference¡¡type£»¡¡because¡¡of¡¡the¡¡constraints¡¡of¡¡using¡¡a¡¡value¡¡type¡£¡¡However£»¡¡for¡¡this¡¡¡¡

example£»¡¡we¡¡will¡¡start¡¡out¡¡by¡¡using¡¡the¡¡Structure¡¡keyword¡¡to¡¡define¡¡Node¡¡as¡¡a¡¡value¡¡type¡£¡¡The¡¡¡¡

depth¡­first¡¡search¡¡algorithm¡¡has¡¡two¡¡distinct¡¡implementation¡¡details£º¡¡data¡¡structure¡¡and¡¡algo

rithm¡£¡¡Because¡¡each¡¡detail¡¡is¡¡separate£»¡¡it¡¡seems¡¡appropriate¡¡to¡¡define¡¡Node¡¡as¡¡a¡¡value¡¡type¡£¡¡So£»¡¡¡¡

least¡¡let¡¯s¡¡try¡¡it¡¡and¡¡see¡¡what¡¡happens¡£¡¡

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