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

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

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



that¡¡does¡¡something¡¡is¡¡like¡¡a¡¡folder¡¡with¡¡stuff¡¡in¡¡it¡£¡¡When¡¡you¡¡are¡¡creating¡¡the¡¡filing¡¡system£»¡¡you¡¡¡¡

don¡¯t¡¡care¡¡about¡¡the¡¡contents¡¡of¡¡the¡¡folder¡£¡¡And¡¡when¡¡you¡¡fill¡¡the¡¡folder£»¡¡you¡¡generally¡¡don¡¯t¡¡¡¡

care¡¡about¡¡the¡¡filing¡¡system¡£¡¡

¡¡¡¡¡¡¡¡¡¡Modules£»¡¡classes£»¡¡namespaces£»¡¡and¡¡methods¡¡are¡¡all¡¡concepts¡¡used¡¡to¡¡organize¡¡source¡¡¡¡

code¡£¡¡A¡¡method¡¡is¡¡filled¡¡with¡¡source¡¡code¡¡and¡¡does¡¡something¡¡like¡¡add¡¡numbers¡¡or¡¡create¡¡a¡¡¡¡

textual¡¡string¡£¡¡

¡¡¡¡¡¡¡¡¡¡One¡¡of¡¡the¡¡most¡¡mon¡¡things¡¡that¡¡you¡¡will¡¡do¡¡when¡¡filling¡¡a¡¡method¡¡with¡¡source¡¡code¡¡is¡¡¡¡

reference¡¡other¡¡pieces¡¡of¡¡organized¡¡source¡¡code¡£¡¡Think¡¡of¡¡referencing¡¡as¡¡putting¡¡a¡¡sticky¡¡note¡¡¡¡

in¡¡a¡¡folder¡¡with¡¡the¡¡text£»¡¡¡°Please¡¡also¡¡look¡¡in¡¡folder¡¡B¡£¡±¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Following¡¡is¡¡a¡¡piece¡¡of¡¡source¡¡code¡¡that¡¡is¡¡100£¥¡¡organizational¡¡and¡¡does¡¡nothing¡£¡¡



Namespace¡¡MyMainTypes¡¡¡¡

¡¡¡¡¡¡Public¡¡Class¡¡AType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Sub¡¡DoSomething£¨£©¡¡

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

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

End¡¡Namespace¡¡



Module¡¡AnotherType¡¡¡¡

¡¡¡¡¡¡¡¡Public¡¡Function¡¡DoSomething£¨£©¡¡As¡¡Integer¡¡

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

End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡source¡¡code¡¡has¡¡three¡¡levels¡¡of¡¡organization¡£¡¡A¡¡namespace¡¡£¨MyMainTypes¡¡in¡¡the¡¡example£©¡¡¡¡

encapsulates¡¡types¡¡like¡¡classes¡¡£¨AType¡¡in¡¡the¡¡example£©¡£¡¡Classes¡¡and¡¡modules¡¡£¨like¡¡AnotherType¡¡¡¡

in¡¡the¡¡example£©¡¡encapsulate¡¡methods¡¡£¨DoSomething£¨£©¡¡in¡¡the¡¡example£©¡¡or¡¡properties¡£¡¡Within¡¡a¡¡¡¡

namespace£»¡¡all¡¡types¡¡must¡¡be¡¡unique¡£¡¡You¡¡can¡¡have¡¡two¡¡types¡¡with¡¡the¡¡same¡¡identifier¡¡in¡¡different¡¡¡¡

namespaces¡£¡¡Within¡¡a¡¡type£»¡¡you¡¡cannot¡¡have¡¡identical¡¡identifiers¡¡with¡¡identical¡¡parameters¡£¡¡¡¡

£¨This¡¡will¡¡be¡¡clearer¡¡as¡¡you¡¡learn¡¡more¡¡about¡¡Visual¡¡Basic¡¡in¡¡the¡¡uping¡¡chapters¡££©¡¡

¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡has¡¡the¡¡additional¡¡rule¡¡that¡¡classes¡¡and¡¡modules¡¡£¨like¡¡AType¡¡and¡¡AnotherType£©¡¡¡¡

are¡¡in¡¡the¡¡namespace¡¡of¡¡the¡¡Visual¡¡Basic¡¡project¡£¡¡That¡¡namespace¡¡is¡¡not¡¡explicitly¡¡defined£»¡¡but¡¡¡¡

it¡¡is¡¡identified¡¡by¡¡the¡¡project£»¡¡as¡¡shown¡¡in¡¡Figure¡¡2¡­6¡£¡¡

¡¡¡¡¡¡¡¡¡¡In¡¡Figure¡¡2¡­6£»¡¡the¡¡Root¡¡Namespace¡¡text¡¡box¡¡shows¡¡the¡¡root¡¡namespace¡¡of¡¡all¡¡types¡¡in¡¡a¡¡¡¡

Visual¡¡Basic¡¡project¡£¡¡So¡¡if¡¡both¡¡AType¡¡and¡¡AnotherType¡¡were¡¡part¡¡of¡¡the¡¡project£»¡¡the¡¡full¡­length¡¡¡¡

identifiers¡¡would¡¡be¡¡Calculator¡£MyMainTypes¡£AType¡¡and¡¡Calculator¡£AnotherType¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡54¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

32¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­6¡£¡¡Default¡¡root¡¡namespace¡¡of¡¡project¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Following¡¡is¡¡the¡¡same¡¡organizational¡¡code¡¡with¡¡some¡¡source¡¡code¡¡added¡¡to¡¡do¡¡something¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨shown¡¡in¡¡boldface£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Namespace¡¡MyMainTypes¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡AType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Sub¡¡DoSomething£¨£©¡¡

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

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Namespace¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module¡¡AnotherType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡DoSomething£¨£©¡¡as¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyMainTypes¡£AType¡£DoSomething£¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡bolded¡¡code£»¡¡there¡¡is¡¡a¡¡reference¡¡to¡¡another¡¡namespace£»¡¡type£»¡¡and¡¡method¡¡with¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pair¡¡of¡¡parentheses¡£¡¡This¡¡makes¡¡a¡¡method¡¡call¡¡on¡¡a¡¡shared¡¡class¡¡and¡¡shared¡¡method¡£¡¡It¡¡says¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡implementation¡¡of¡¡the¡¡method¡¡is¡¡the¡¡calling¡¡of¡¡another¡¡method¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡55¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡33¡¡



¡¡¡¡¡¡¡¡¡¡Notice¡¡how¡¡the¡¡other¡¡method¡¡is¡¡referenced¡¡using¡¡both¡¡namespace¡¡and¡¡type¡¡identifiers¡£¡¡Also¡¡¡¡

notice¡¡how¡¡there¡¡is¡¡no¡¡reference¡¡to¡¡the¡¡Calculator¡¡namespace¡£¡¡This¡¡is¡¡not¡¡necessary¡¡because£»¡¡¡¡

from¡¡the¡¡perspective¡¡of¡¡AnotherType£»¡¡Calculator¡¡is¡¡implied¡£¡¡This¡¡is¡¡how¡¡all¡¡types¡¡and¡¡methods¡¡¡¡

are¡¡always¡¡referenced¡£¡¡A¡¡namespace¡¡identifier¡¡is¡¡necessary¡¡only¡¡if¡¡the¡¡type¡¡£¨for¡¡example£»¡¡class£©¡¡¡¡

is¡¡not¡¡defined¡¡in¡¡the¡¡current¡¡namespace¡£¡¡

¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡have¡¡namespaces¡¡with¡¡long¡¡names£»¡¡this¡¡referencing¡¡can¡¡get¡¡tedious¡£¡¡As¡¡an¡¡alterna

tive£»¡¡you¡¡can¡¡add¡¡an¡¡Imports¡¡statement¡¡to¡¡reference¡¡the¡¡namespace£»¡¡similar¡¡to¡¡the¡¡following¡£¡¡



Imports¡¡Calculator¡£MyMainTypes¡¡

Module¡¡AnotherType¡¡¡¡

¡¡¡¡¡¡¡¡Public¡¡Function¡¡DoSomething£¨£©¡¡as¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡AType¡£DoSomething£¨£©¡¡

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

End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡Imports¡¡statement¡¡says¡¡that¡¡if¡¡the¡¡code¡¡references¡¡any¡¡types¡¡that¡¡are¡¡not¡¡defined¡¡locally£»¡¡¡¡

look¡¡in¡¡this¡¡namespace¡¡£¨Calculator¡£MyMainTypes¡¡in¡¡the¡¡example£©¡¡to¡¡find¡¡the¡¡type¡£¡¡When¡¡using¡¡¡¡

the¡¡Imports¡¡statement£»¡¡you¡¡need¡¡to¡¡specify¡¡the¡¡full¡¡namespace¡¡of¡¡Calculator¡£MyMainTypes¡£¡¡¡¡

Note¡¡that¡¡if¡¡you¡¡use¡¡two¡¡namespaces¡¡that¡¡have¡¡identically¡¡named¡¡types£»¡¡you¡¡will¡¡get¡¡a¡¡piler¡¡¡¡

failure£»¡¡because¡¡the¡¡piler¡¡won¡¯t¡¡know¡¡which¡¡type¡¡to¡¡reference¡£¡¡

¡¡¡¡¡¡¡¡¡¡This¡¡covers¡¡the¡¡absolute¡¡basics¡¡of¡¡writing¡¡some¡¡code£»¡¡and¡¡we¡¡are¡¡ready¡¡to¡¡use¡¡Visual¡¡Basic¡¡¡¡

to¡¡do¡¡something¡£¡¡



Writing¡¡the¡¡Add£¨£©¡¡Method¡¡



We¡¯ll¡¡write¡¡the¡¡code¡¡to¡¡add¡¡two¡¡numbers¡£¡¡To¡¡begin£»¡¡create¡¡a¡¡new¡¡project¡¡in¡¡Visual¡¡Basic£º¡¡



¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Open¡¡Visual¡¡Basic¡¡Express¡£¡¡£¨If¡¡Visual¡¡Basic¡¡Express¡¡is¡¡open£»¡¡choose¡¡File¡¡¡¡Close¡¡Project¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡ensure¡¡you¡¡have¡¡a¡¡clean¡¡slate¡££©¡¡



¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Click¡¡File¡¡¡¡New¡¡Project¡¡or¡¡choose¡¡Create£º¡¡Project¡¡from¡¡the¡¡Start¡¡Page¡¡tab¡£¡¡



¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Choose¡¡Class¡¡Library£»¡¡name¡¡it¡¡Calculator£»¡¡and¡¡click¡¡OK¡£¡¡



¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Rename¡¡Class1¡£vb¡¡to¡¡Operations¡£vb¡£¡¡



¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡Save¡¡the¡¡solution¡£¡¡



¡¡¡¡¡¡¡¡¡¡We¡¡can¡¡now¡¡write¡¡the¡¡Add£¨£©¡¡method¡£¡¡Add¡¡the¡¡bolded¡¡code¡¡to¡¡the¡¡Operations¡£vb¡¡file¡£¡¡



Public¡¡Class¡¡Operations¡¡

¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Function¡¡Add£¨ByVal¡¡number1¡¡As¡¡Integer£»¡¡ByVal¡¡number2¡¡As¡¡_¡¡

Integer£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡number1¡¡£«¡¡number2¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡This¡¡simple¡¡code¡¡actually¡¡has¡¡many¡¡different¡¡pieces¡¡that¡¡fit¡¡together¡£¡¡The¡¡type¡¡Operations¡¡¡¡

is¡¡implied¡¡to¡¡be¡¡in¡¡the¡¡namespace¡¡Calculator¡¡because¡¡the¡¡default¡¡root¡¡namespace¡¡is¡¡the¡¡same¡¡¡¡

as¡¡the¡¡project¡£¡¡Defined¡¡within¡¡Operations¡¡is¡¡a¡¡method¡¡that¡¡is¡¡both¡¡Public¡¡and¡¡Shared¡£¡¡Using¡¡the¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡56¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

34¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function¡¡declaration¡¡implies¡¡that¡¡the¡¡caller¡¡expects¡¡to¡¡get¡¡a¡¡value¡¡back¡¡from¡¡the¡¡method¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡type¡¡of¡¡the¡¡returned¡¡value¡¡is¡¡specified¡¡by¡¡the¡¡As¡¡Integer¡¡keywords¡ªthis¡¡particular¡¡func

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tion¡¡will¡¡return¡¡an¡¡Integer¡¡value¡£¡¡£¨If¡¡you¡¡want¡¡to¡¡define¡¡a¡¡method¡¡that¡¡does¡¡not¡¡return¡¡a¡¡value£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡use¡¡Sub¡¡rather¡¡than¡¡¡¡Function¡££©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Methods¡¡and¡¡parameters¡¡must¡¡be¡¡associated¡¡with¡¡a¡¡type£»¡¡as¡¡Visual¡¡Basic¡¡is¡¡a¡¡type¡­safe¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡programming¡¡language¡£¡¡¡¡Type¡­safe¡¡means¡¡that¡¡when¡¡you¡¡write¡¡code£»¡¡you¡¡know¡¡what¡¡you¡¡are¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡manipulating¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Suppose¡¡that¡¡you¡¡are¡¡writing¡¡code¡¡and¡¡are¡¡confronted¡¡with¡¡the¡¡numbers¡¡1£»¡¡1¡£0£»¡¡and¡¡¡¨1¡£0¡¨¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡you£»¡¡these¡¡three¡¡numbers¡¡are¡¡identical¡£¡¡But¡¡in¡¡the¡¡context¡¡of¡¡the¡¡source¡¡code£»¡¡they¡¡are¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡identical¡£¡¡The¡¡1¡¡is¡¡an¡¡integer£»¡¡the¡¡¡¡1¡£0¡¡is¡¡a¡¡double£»¡¡and¡¡the¡¡¡¡¡¨1¡£0¡¨¡¡is¡¡a¡¡string¡£¡¡When¡¡you¡¡want¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡add£»¡¡subtract£»¡¡or¡¡otherwise¡¡manipulate¡¡pieces¡¡of¡¡data£»¡¡they¡¡should¡¡be¡¡the¡¡same¡¡types£»¡¡other

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡wise£»¡¡you¡¡might¡¡run¡¡into¡¡consistency¡¡errors¡£¡¡Type¡­safe¡¡programming¡¡languages¡¡help¡¡avoid¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡such¡¡problems¡£¡¡The¡¡¡¡number¡¡types¡¡are¡¡discussed¡¡in¡¡more¡¡detail¡¡in¡¡the¡¡¡°Understanding¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CLR¡¡Numeric¡¡Types¡±¡¡section¡¡later¡¡in¡¡this¡¡chapter¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡declaration¡¡of¡¡Add£¨£©¡¡says¡¡that¡¡we¡¡need¡¡to¡¡pass¡¡in¡¡two¡¡integer¡­based¡¡numeric¡¡values£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡the¡¡method¡¡returns¡¡an¡¡integer¡­based¡¡numeric¡¡value¡£¡¡The¡¡bination¡¡of¡¡parameters¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡return¡¡type¡¡is¡¡a¡¡method¡¡signature¡£¡¡The¡¡method¡¡signature¡¡bees¡¡important¡¡when¡¡another¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡piece¡¡of¡¡code¡¡calls¡¡the¡¡Add£¨£©¡¡method¡£¡¡The¡¡other¡¡piece¡¡of¡¡code¡¡must¡¡use¡¡the¡¡same¡¡types¡¡as¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡declaration¡£¡¡Figure¡¡2¡­7¡¡shows¡¡a¡¡piece¡¡of¡¡code¡¡that¡¡calls¡¡the¡¡Add£¨£©¡¡method£»¡¡which¡¡we¡¯ll¡¡do¡¡from¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡another¡¡application¡¡in¡¡the¡¡next¡¡section¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Namespace¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Method¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡total¡¡As¡¡Integer¡¡=¡¡Calculator¡£Operations¡£Add£¨1£»¡¡2£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡total¡¡is¡¡an¡¡integer¡¡declared¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡¡and¡¡2¡¡are¡¡integer¡¡values£»¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡that¡¡stores¡¡the¡¡result¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡represent¡¡the¡¡two¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡addition¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡numbers¡¡that¡¡are¡¡added¡¡together¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­7¡£¡¡¡¡The¡¡Add£¨£©¡¡method¡¡is¡¡called¡¡by¡¡referencing¡¡the¡¡namespace¡¡and¡¡class¡¡containing¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡method¡£¡¡A¡¡period¡¡is¡¡used¡¡to¡¡separate¡¡the¡¡identifiers¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡caller¡¡must¡¡do¡¡two¡¡things£º¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Reference¡¡the¡¡correct¡¡bination¡¡of¡¡namespace£»¡¡class£»¡¡and¡¡method¡¡identifiers¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Pass¡¡the¡¡correct¡¡types¡¡into¡¡the¡¡method£»¡¡as¡¡specified¡¡by¡¡the¡¡method¡¡signature¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡addition¡¡of¡¡1¡¡and¡¡2¡¡results¡¡in¡¡3£»¡¡and¡¡therefore¡¡the¡¡variable¡¡total¡¡should¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contain¡¡the¡¡value¡¡3¡¡£¨the¡¡equal¡¡sign¡¡assigns¡¡the¡¡value¡¡returned¡¡from¡¡the¡¡method¡¡to¡¡the¡¡variable¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on¡¡its¡¡left£©¡£¡¡I¡¡say¡¡¡°should¡¡contain¡¡the¡¡value£»¡±¡¡because¡¡when¡¡writing¡¡code£»¡¡you¡¡are¡¡not¡¡always¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sure¡£¡¡Sometimes¡¡the¡¡code¡¡you¡¡write¡¡will¡¡be¡¡wrong¡¡because¡¡you¡¡overlooked¡¡something¡¡or¡¡forgot¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡reference¡¡something¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Look¡¡at¡¡the¡¡calling¡¡code£»¡¡and¡¡ask¡¡yourself¡¡if¡¡you¡¡are¡¡guaranteed¡¡that¡¡calling¡¡Add£¨£©¡¡with¡¡¡¡1¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡2¡¡will¡¡result¡¡in¡¡3¡£¡¡The¡¡answer¡¡is¡¡that£»¡¡as¡¡a¡¡caller£»¡¡you¡¡cannot¡¡be¡¡100£¥¡¡sure¡¡that¡¡the¡¡total¡¡variable¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡will¡¡contain¡¡3¡£¡¡Just¡¡because¡¡a¡¡box¡¡has¡¡the¡¡label¡¡¡°Dishes¡±¡¡does¡¡not¡¡necessarily¡¡mean¡¡that¡¡dishes¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡57¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡35¡¡



are¡¡in¡¡the¡¡box¡£¡¡You¡¡think¡¡you¡¡know¡¡the¡¡contents£»¡¡but¡¡you¡¡cannot¡¡be¡¡100£¥¡¡sure¡¡until¡¡you¡¡open¡¡¡¡

the¡¡box¡£¡¡Likewise£»¡¡in¡¡code£»¡¡you¡¡need¡¡to¡¡look¡¡at¡¡how¡¡the¡¡Add£¨£©¡¡method¡¡is¡¡implemented¡¡to¡¡be¡¡sure¡¡¡¡

of¡¡the¡¡contents¡¡of¡¡the¡¡total¡¡variable¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡In¡¡a¡¡production¡¡coding¡¡session£»¡¡looking¡¡at¡¡the¡¡implementation¡¡code¡¡to¡¡verify¡¡it¡¡is¡¡doing¡¡¡¡

what¡¡you¡¡expect¡¡is¡¡not¡¡a¡¡feasible¡¡solution£»¡¡because¡¡that¡¡would¡¡take¡¡too¡¡much¡¡time¡¡and¡¡be¡¡¡¡

pletely¡¡unreliable¡£¡¡The¡¡only¡¡real¡¡solution¡¡is¡¡to¡¡write¡¡test¡¡code¡£¡¡¡¡



Writing¡¡Code¡¡to¡¡Test¡¡the¡¡Add£¨£©¡¡Method¡¡



Test¡¡code¡¡is¡¡caller¡¡code¡¡that¡¡passes¡¡parameters¡¡with¡¡targeted¡¡values¡¡and¡¡expects¡¡a¡¡targeted¡¡¡¡

answer¡£¡¡If¡¡the¡¡caller¡¡does¡¡not¡¡get¡¡the¡¡targeted¡¡answer£»¡¡then¡¡the¡¡implementation¡¡of¡¡the¡¡tested¡¡¡¡

method¡¡is¡¡wrong¡£¡¡Figure¡¡2¡­8¡¡shows¡¡sample¡¡caller¡¡code¡¡that¡¡tests¡¡the¡¡Add£¨£©¡¡operation¡¡£¨we¡¯ll¡¡add¡¡¡¡

this¡¡to¡¡a¡¡project¡¡next£©¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Targeted¡¡caller¡¡code¡¡that¡¡adds¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡¡and¡¡2¡¡and¡¡assigns¡¡the¡¡result¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡variable¡¡total¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡total¡¡As¡¡Integer¡¡=¡¡Operations¡£Add£¨1£»¡¡2£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨total¡¡¡¡3£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Oops¡¡1¡¡add¡¡2¡¡does¡¡not¡¡equal¡¡3¡¨£©¡¡

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



¡¡¡¡¡¡Targeted¡¡testing¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡targeted¡¡testing¡¡fails£»¡¡the¡¡

¡¡¡¡¡¡¡¡of¡¡the¡¡variable¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡text¡¡¡°Oops¡­¡±¡¡is¡¡generated£»¡¡

¡¡¡¡¡¡total£»¡¡paring¡¡it¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡indicating¡¡an¡¡error¡¡

¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡value¡¡3¡¡



Figure¡¡2¡­8¡£¡¡¡¡Testing¡¡the¡¡Add£¨£©¡¡method¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡calling¡¡code¡¡of¡¡the¡¡test¡¡bears¡¡an¡¡uncanny¡¡resemblance¡¡to¡¡the¡¡code¡¡you¡¡saw¡¡in¡¡the¡¡previous¡¡¡¡

section¡£¡¡The¡¡difference¡¡is¡¡that¡¡the¡¡test¡¡code¡¡uses¡¡targeted¡¡variables¡¡and¡¡values£»¡¡whereas¡¡the¡¡¡¡

other¡¡code¡¡could¡¡contain¡¡any¡¡variables¡¡and¡¡values¡£¡¡Another¡¡requirement¡¡of¡¡test¡¡code¡¡is¡¡to¡¡verify¡¡¡¡

the¡¡answers¡¡returned¡¡by¡¡the¡¡method¡¡with¡¡targeted¡¡responses¡£¡¡The¡¡If¡¡statement¡¡is¡¡used¡¡to¡¡check¡¡¡¡

whether¡¡the¡¡value¡¡of¡¡the¡¡variable¡¡total¡¡is¡¡equal¡¡to¡¡¡¡3¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡When¡¡writing¡¡test¡¡code£»¡¡the¡¡way¡¡the¡¡Add£¨£©¡¡method¡¡is¡¡used¡¡must¡¡be¡¡the¡¡same¡¡way¡¡the¡¡¡¡

console¡¡or¡¡Windows¡¡application¡¡uses¡¡the¡¡method¡£¡¡Otherwise£»¡¡it¡¡would¡¡be¡¡like¡¡testing¡¡a¡¡winter¡¡¡¡

tire¡¡in¡¡the¡¡middle¡¡of¡¡the¡¡Sahara¡ªfun¡¡to¡¡do¡¡but¡¡irrelevant¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Another¡¡question¡¡related¡¡to¡¡testing¡¡has¡¡to¡¡do¡¡with¡¡the¡¡timing¡¡of¡¡tests¡£¡¡Do¡¡you¡¡create¡¡the¡¡¡¡

tests¡¡before¡¡or¡¡after¡¡implementing¡¡the¡¡Add£¨£©¡¡method£¿¡¡To¡¡get¡¡a¡¡clear¡¡understanding¡¡of¡¡the¡¡problem£»¡¡¡¡

imagine¡¡the¡¡development¡¡of¡¡a¡¡tire¡£¡¡Do¡¡you¡¡define¡¡the¡¡tests¡¡for¡¡the¡¡tire¡¡before¡¡or¡¡after¡¡the¡¡tire¡¡has¡¡¡¡

been¡¡developed£¿¡¡Most¡¡likely£»¡¡the¡¡answer¡¡is¡¡before£»¡¡during£»¡¡and¡¡after¡¡development¡£¡¡This¡¡is¡¡an¡¡¡¡

important¡¡consideration¡¡when¡¡developing¡¡software¡£¡¡Tests¡¡are¡¡written¡¡before£»¡¡during£»¡¡and¡¡after¡¡¡¡

implementation£»¡¡as¡¡follows£º¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡58¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

36¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡before¡¡implementing¡¡the¡¡Add£¨£©¡¡method¡¡to¡¡get¡¡an¡¡idea¡¡of¡¡what¡¡name

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡spaces£»¡¡classes£»¡¡and¡¡methods¡¡you¡¡will¡¡be¡¡defining¡£¡¡The¡¡definition¡¡of¡¡the¡¡different¡¡items¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡gives¡¡the¡¡developer¡¡an¡¡idea¡¡of¡¡how¡¡the¡¡items¡¡will¡¡be¡¡used¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡during¡¡the¡¡implementation¡¡of¡¡the¡¡Add£¨£©¡¡method¡¡to¡¡verify¡¡that¡¡your¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡source¡¡code¡¡implementation¡¡is¡¡on¡¡the¡¡right¡¡track¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡after¡¡the¡¡implementation¡¡of¡¡the¡¡Add£¨£©¡¡method¡¡as¡¡an¡¡exhaustive¡¡measure¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡make¡¡sure¡¡you¡¯ve¡¡dotted¡¡the¡¡i¡¯s¡¡and¡¡crossed¡¡the¡¡t¡¯s¡¡in¡¡the¡¡implementation¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡a¡¡Test¡¡Project¡¡to¡¡Your¡¡Solution¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡writing¡¡test¡¡routines£»¡¡you¡¡will¡¡need¡¡to¡¡organize¡¡the¡¡source¡¡code£»¡¡and¡¡that¡¡means¡¡figuring¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡out¡¡to¡¡which¡¡project¡¡the¡¡tests¡¡are¡¡added¡£¡¡For¡¡the¡¡calculator¡¡application£»¡¡you¡¡could¡¡place¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡within¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡However£»¡¡doing¡¡that¡¡is¡¡not¡¡the¡¡proper¡¡approach¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡due¡¡to¡¡distribution¡¡of¡¡the¡¡class¡¡library¡¡and¡¡correct¡¡testing¡¡context¡£¡¡Remember¡¡that¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡must¡¡be¡¡identical¡¡to¡¡how¡¡the¡¡code¡¡will¡¡be¡¡used¡£¡¡Thus£»¡¡the¡¡appropriate¡¡place¡¡for¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡is¡¡in¡¡their¡¡own¡¡application¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡ideal¡¡approach¡¡is¡¡to¡¡create¡¡another¡¡application¡¡that¡¡represents¡¡the¡¡tests¡£¡¡Figure¡¡2¡­5¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡illustrated¡¡how¡¡a¡¡Windows¡¡and¡¡console¡¡application¡¡could¡¡use¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­9¡¡adds¡¡the¡¡testing¡¡console¡¡application¡¡that¡¡also¡¡uses¡¡the¡¡class¡¡library¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­9¡£¡¡Adding¡¡the¡¡testing¡¡console¡¡application¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡testing¡¡console¡¡application¡¡is¡¡like¡¡the¡¡console¡¡application¡¡created¡¡in¡¡Chapter¡¡1£»¡¡and¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡references¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡Both¡¡projects¡¡should¡¡be¡¡part¡¡of¡¡the¡¡Calculator¡¡solution¡£¡¡¡¡

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