VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ26²¿·Ö
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
¡¡¡¡¡¡¡¡¡¡¡¡As¡¡shown¡¡in¡¡Figure¡¡4¡3£»¡¡you¡¡can¡¡choose¡¡to¡¡create¡¡a¡¡user¡defined¡¡type¡¡as¡¡a¡¡value¡¡type¡¡¡¡
£¨Structure£©¡¡or¡¡reference¡¡type¡¡£¨Class£©¡¡type¡£¡¡For¡¡the¡¡most¡¡part£»¡¡developers¡¡use¡¡a¡¡reference¡¡type£»¡¡¡¡
because¡¡it¡¡has¡¡fewer¡¡constraints¡¡and¡¡is¡¡easiest¡¡to¡¡use¡¡in¡¡a¡¡general¡¡context¡£¡¡A¡¡value¡¡type¡¡has¡¡some¡¡¡¡
constraints¡¡due¡¡its¡¡behavior¡¡of¡¡storing¡¡everything¡¡on¡¡the¡¡stack£»¡¡as¡¡discussed¡¡in¡¡the¡¡following¡¡¡¡
sections¡£¡¡
Value¡¡Type¡¡Constraints¡¡
The¡¡constraints¡¡of¡¡using¡¡a¡¡value¡¡type¡¡relate¡¡to¡¡the¡¡fact¡¡that¡¡data¡¡is¡¡copied¡£¡¡This¡¡has¡¡an¡¡impact¡¡on¡¡¡¡
what¡¡happens¡¡when¡¡you¡¡embed¡¡reference¡¡types¡¡in¡¡value¡¡types¡¡and¡¡use¡¡value¡¡types¡¡as¡¡parame
ters¡¡to¡¡methods¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡104¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
82¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡Effects¡¡of¡¡Copying¡¡Data¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡one¡¡¡¡value¡¡type¡¡is¡¡assigned¡¡to¡¡another¡¡value¡¡type£»¡¡the¡¡contents¡¡of¡¡the¡¡value¡¡types¡¡are¡¡copied¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡see¡¡this¡¡in¡¡action£»¡¡consider¡¡the¡¡declarations¡¡in¡¡Figure¡¡4¡4¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaration¡¡of¡¡the¡¡identifier¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Custom¡¡value¡¡type¡¡declaration¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡is¡¡inside¡¡the¡¡block£»¡¡so¡¡it¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡belongs¡¡to¡¡the¡¡custom¡¡type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Structure¡¡MyValueType¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaration¡¡of¡¡value¡¡is¡¡associated¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Structure¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡a¡¡type¡¡identifier¡¡£¨Integer£©¡¡and¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡MyReferenceType¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡scope¡¡identifier¡¡£¨Public£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Custom¡¡reference¡¡type¡¡declaration¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Both¡¡custom¡¡types¡¡declare¡¡a¡¡variable¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡is¡¡a¡¡value¡¡type£»¡¡which¡¡is¡¡called¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡member¡¡because¡¡it¡¡belongs¡¡to¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡custom¡¡type¡¡declaration¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡4¡£¡¡Declaring¡¡custom¡¡types¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡declaring¡¡user¡defined¡¡types£»¡¡the¡¡data¡¡members¡¡and¡¡methods¡¡are¡¡declared¡¡inside¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡block¡¡of¡¡code¡£¡¡You¡¡can¡¡think¡¡of¡¡the¡¡declaration¡¡as¡¡the¡¡writing¡¡on¡¡the¡¡outside¡¡of¡¡the¡¡box£»¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡anything¡¡in¡¡the¡¡block¡¡of¡¡code¡¡as¡¡the¡¡contents¡¡of¡¡the¡¡box¡£¡¡The¡¡custom¡¡types¡¡£¨MyValueType¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyReferenceType£©¡¡as¡¡declared¡¡in¡¡Figure¡¡4¡4¡¡do¡¡not¡¡have¡¡a¡¡scope¡¡identifier¡£¡¡Think¡¡of¡¡a¡¡scope¡¡identifier¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡defining¡¡who¡¡has¡¡access¡¡to¡¡your¡¡pockets¡¡and¡¡wallet¡£¡¡In¡¡the¡¡case¡¡of¡¡the¡¡types¡¡in¡¡the¡¡example£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡scope¡¡is¡¡like¡¡saying¡¡that¡¡your¡¡spouse¡¡is¡¡allowed¡¡to¡¡peek¡¡into¡¡your¡¡wallet£»¡¡but¡¡strangers¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cannot¡£¡¡In¡¡other¡¡words£»¡¡the¡¡types¡¡cannot¡¡be¡¡used¡¡outside¡¡their¡¡own¡¡namespace¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡Public¡¡keyword¡¡had¡¡been¡¡in¡¡front¡¡of¡¡the¡¡type¡¡identifier£»¡¡then¡¡the¡¡user¡defined¡¡type¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡would¡¡be¡¡exposed¡¡like¡¡a¡¡wallet¡¡that¡¡is¡¡allowed¡¡to¡¡be¡¡peeked¡¡into¡¡by¡¡the¡¡general¡¡public¡£¡¡This¡¡means¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡types¡¡can¡¡be¡¡used¡¡by¡¡other¡¡code¡£¡¡In¡¡the¡¡case¡¡of¡¡the¡¡wallet£»¡¡it¡¯s¡¡a¡¡bad¡¡idea¡¡to¡¡have¡¡unfettered¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡public¡¡scope£»¡¡but¡¡sometimes¡¡public¡¡scope¡¡is¡¡desirable¡¡when¡¡you¡¡are¡¡able¡¡to¡¡control¡¡its¡¡access¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡And¡¡you¡¡do¡¡this¡¡every¡¡time¡¡you¡¡pay¡¡for¡¡something¡¡by¡¡handing¡¡a¡¡credit¡¡card¡¡to¡¡the¡¡cashier¡£¡¡In¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡case£»¡¡you¡¡are¡¡publicly¡¡exposing¡¡parts¡¡of¡¡your¡¡wallet¡¡under¡¡your¡¡supervision¡£¡¡In¡¡code¡¡terms£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this¡¡means¡¡making¡¡your¡¡types¡¡public£»¡¡but¡¡not¡¡necessarily¡¡making¡¡the¡¡data¡¡members¡¡public¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next£»¡¡consider¡¡the¡¡code¡¡in¡¡Figure¡¡4¡5¡£¡¡It¡¡instantiates¡¡MyValueType¡¡and¡¡assigns¡¡it¡¡to¡¡another¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡of¡¡the¡¡same¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡example¡¡in¡¡Figure¡¡4¡5¡¡illustrates¡¡what¡¡happens¡¡to¡¡two¡¡variables¡¡when¡¡one¡¡is¡¡instanti
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ated¡¡and¡¡assigned¡¡to¡¡the¡¡other£»¡¡and¡¡then¡¡the¡¡other¡¡is¡¡modified¡£¡¡You¡¡want¡¡to¡¡understand¡¡how¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡each¡¡data¡¡type¡¡is¡¡modified¡¡when¡¡an¡¡interaction¡¡with¡¡another¡¡type¡¡occurs¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡105¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡83¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡create¡¡a¡¡valid¡¡instance¡¡
¡¡¡¡¡¡¡¡Variable¡¡var¡¡is¡¡declared£»¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡associated¡¡with¡¡the¡¡
¡¡¡¡¡¡but¡¡a¡¡declaration¡¡does¡¡not¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡var£»¡¡you¡¡need¡¡to¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Another¡¡way¡¡to¡¡associate¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡imply¡¡a¡¡valid¡¡instance¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡allocate¡¡the¡¡object¡¡using¡¡¡¡¡¡¡¡¡¡¡¡¡¡valid¡¡instance¡¡with¡¡a¡¡variable¡¡is¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡New¡¡keyword¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡assign¡¡the¡¡variable¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨e¡£g¡££»¡¡copiedVar£©¡¡from¡¡another¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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