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

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

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




¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Rename¡¡the¡¡file¡¡Translator¡£vb¡£¡¡



¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡Click¡¡Add¡¡to¡¡create¡¡the¡¡file¡¡and¡¡add¡¡it¡¡to¡¡your¡¡project¡£¡¡



¡¡¡¡¡¡¡¡¡¡Notice¡¡how¡¡quickly¡¡you¡¡managed¡¡to¡¡create¡¡a¡¡Visual¡¡Basic¡¡class¡¡using¡¡the¡¡IDE¡£¡¡The¡¡speed¡¡of¡¡¡¡

creating¡¡a¡¡class¡¡file¡¡lets¡¡you¡¡focus¡¡on¡¡adding¡¡source¡¡code¡¡to¡¡the¡¡file¡£¡¡But¡¡do¡¡not¡¡be¡¡misled¡¡into¡¡¡¡

believing¡¡that¡¡by¡¡creating¡¡a¡¡number¡¡of¡¡class¡¡files£»¡¡your¡¡code¡¡will¡¡automatically¡¡work¡¡and¡¡be¡¡a¡¡¡¡

masterpiece¡£¡¡You¡¡still¡¡need¡¡to¡¡think¡¡about¡¡which¡¡files£»¡¡projects£»¡¡classes£»¡¡and¡¡tests¡¡to¡¡create¡£¡¡



Translating¡¡Hello¡¡



The¡¡first¡¡feature¡¡we¡¡will¡¡implement¡¡is¡¡the¡¡translation¡¡of¡¡the¡¡text¡¡¡°hello¡£¡±¡¡Since¡¡¡°hello¡±¡¡is¡¡English£»¡¡¡¡

the¡¡first¡¡translation¡¡will¡¡be¡¡English¡¡to¡¡German¡£¡¡The¡¡following¡¡is¡¡the¡¡code¡¡to¡¡implement¡¡this¡¡¡¡

feature¡£¡¡It¡¡is¡¡added¡¡to¡¡the¡¡Translator¡£vb¡¡file¡¡in¡¡the¡¡¡¡LanguageTranslator¡¡project¡£¡¡



Public¡¡Class¡¡Translator¡¡

¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Function¡¡TranslateHello£¨ByVal¡¡input¡¡As¡¡String£©¡¡As¡¡String¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨input¡£pareTo£¨¡¨hello¡¨£©¡¡=¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡¡¨hallo¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡£¨input¡£pareTo£¨¡¨allo¡¨£©¡¡=¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡¡¨hallo¡¨¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡

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



End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡Translator¡¡is¡¡the¡¡main¡¡class¡¡that¡¡is¡¡exposed¡¡to¡¡other¡¡ponents¡¡or¡¡pieces¡¡of¡¡source¡¡code¡£¡¡¡¡

Think¡¡of¡¡it¡¡as¡¡the¡¡identifier¡¡of¡¡the¡¡black¡¡box¡£¡¡The¡¡black¡¡box¡¡has¡¡a¡¡single¡¡method£º¡¡TranslateHello£¨£©¡£¡¡¡¡

TranslateHello£¨£©¡¡is¡¡used¡¡to¡¡convert¡¡the¡¡French¡¡¡°allo¡±¡¡and¡¡the¡¡English¡¡¡°hello¡±¡¡to¡¡the¡¡German¡¡¡¡

¡°hallo¡£¡±¡¡The¡¡method¡¯s¡¡input¡¡is¡¡a¡¡String¡¡type£»¡¡which¡¡is¡¡a¡¡reference¡¡object¡¡type¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡implementation¡¡of¡¡TranslateHello£¨£©£»¡¡we¡¡pare¡¡the¡¡contents¡¡of¡¡the¡¡input¡¡buffer¡¡¡¡

to¡¡the¡¡parameter¡¡¡¡¡¨hello¡¨¡£¡¡If¡¡the¡¡parison¡¡is¡¡equal£»¡¡meaning¡¡that¡¡the¡¡strings¡¡are¡¡equal£»¡¡0¡¡is¡¡¡¡

returned¡£¡¡As¡¡you¡¯ll¡¡learn¡¡in¡¡the¡¡¡°Investigating¡¡the¡¡String¡¡Type¡±¡¡section£»¡¡String¡¡is¡¡a¡¡class¡¡that¡¡can¡¡¡¡

be¡¡used¡¡to¡¡create¡¡objects£»¡¡and¡¡objects¡¡typically¡¡have¡¡methods¡£¡¡One¡¡of¡¡the¡¡String¡¡type¡¯s¡¡methods¡¡¡¡

is¡¡pareTo£¨£©¡£¡¡The¡¡caller¡¡of¡¡TranslateHello¡¡does¡¡not¡¡know¡¡how¡¡you¡¡managed¡¡to¡¡translate¡¡one¡¡¡¡

word¡¡to¡¡another¡¡language¡£¡¡The¡¡caller¡¡actually¡¡does¡¡not¡¡care£»¡¡it¡¡cares¡¡only¡¡that¡¡the¡¡method¡¡behaves¡¡¡¡

as¡¡expected¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡abstract¡¡intent¡¡of¡¡the¡¡TranslateHello£¨£©¡¡method¡¡is¡¡to¡¡accept¡¡some¡¡text¡¡and£»¡¡if¡¡the¡¡text¡¡¡¡

is¡¡matched£»¡¡return¡¡a¡¡German¡¡¡°hallo¡£¡±¡¡



Creating¡¡the¡¡Test¡¡Application¡¡



Without¡¡questioning¡¡the¡¡abstract¡¡intent£»¡¡the¡¡written¡¡code¡¡needs¡¡some¡¡testing¡£¡¡The¡¡test¡¡code¡¡is¡¡¡¡

added¡¡to¡¡the¡¡test¡¡application£»¡¡which¡¡is¡¡the¡¡project¡¡TestLanguageTranslator¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡code¡¡is¡¡added¡¡to¡¡the¡¡Module1¡£vb¡¡file¡£¡¡Remember¡¡to¡¡import¡¡the¡¡¡¡

LanguageTranslator¡¡namespace£»¡¡otherwise£»¡¡the¡¡test¡¡code¡¡will¡¡not¡¡pile¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡76¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

54¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡LanguageTranslator¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module¡¡Module1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Sub¡¡TestTranslateHello£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨Translator¡£TranslateHello£¨¡¨hello¡¨£©¡£pareTo£¨¡¨hallo¡¨£©¡¡¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨hello¡¡to¡¡hallo¡¡test¡¡failed¡¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨Translator¡£TranslateHello£¨¡¨allo¡¨£©¡£pareTo£¨¡¨hallo¡¨£©¡¡¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨allo¡¡to¡¡hallo¡¡test¡¡failed¡¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨Translator¡£TranslateHello£¨¡¨allosss¡¨£©¡£pareTo£¨¡¨¡¨£©¡¡¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Verify¡¡nontranslated¡¡word¡¡test¡¡failed¡¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨Translator¡£TranslateHello£¨¡¨¡¡¡¡allo¡¨£©¡£pareTo£¨¡¨hallo¡¨£©¡¡¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Extra¡¡whitespaces¡¡allo¡¡to¡¡hallo¡¡test¡¡failed¡¨£©¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestTranslateHello£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

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



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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡source¡¡code¡¡contains¡¡four¡¡tests¡£¡¡Each¡¡calls¡¡the¡¡method¡¡TranslateHello£¨£©¡¡with¡¡some¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡input¡¡and¡¡receives¡¡the¡¡output¡£¡¡The¡¡test¡¡happens¡¡when¡¡the¡¡output¡¡is¡¡verified¡¡against¡¡an¡¡expected¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡result¡£¡¡The¡¡verification¡¡tests¡¡use¡¡the¡¡pareTo£¨£©¡¡function¡¡to¡¡test¡¡for¡¡correct¡¡translation¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Notice¡¡the¡¡third¡¡test£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨Translator¡£TranslateHello£¨¡¨allosss¡¨£©¡£pareTo£¨¡¨¡¨£©¡¡¡¡0£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Verify¡¡nontranslated¡¡word¡¡test¡¡failed¡¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡test¡¡expects¡¡an¡¡explicit¡¡failure¡£¡¡You¡¡need¡¡to¡¡write¡¡tests¡¡that¡¡expect¡¡to¡¡fail¡£¡¡Successful¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tests¡¡that¡¡are¡¡meant¡¡to¡¡fail¡¡ensure¡¡that¡¡your¡¡code¡¡does¡¡not¡¡generate¡¡false¡¡positives¡£¡¡A¡¡false¡¡positive¡¡¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡when¡¡your¡¡code¡¡is¡¡supposed¡¡to¡¡fail¡¡and¡¡it¡¡does¡¡not¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡tests¡¡are¡¡within¡¡a¡¡method¡¡that¡¡needs¡¡to¡¡be¡¡called¡¡from¡¡the¡¡Main£¨£©¡¡method£»¡¡as¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡following¡¡example£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestTranslateHello£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡pile¡¡and¡¡run¡¡the¡¡tests£»¡¡you¡¡will¡¡find¡¡that¡¡one¡¡of¡¡the¡¡tests¡¡fails¡£¡¡The¡¡failing¡¡test¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡fourth¡¡one£»¡¡which¡¡attempts¡¡to¡¡translate¡¡a¡¡word¡¡that¡¡has¡¡whitespace¡£¡¡Whitespace¡¡is¡¡space¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡we¡¡humans¡¡don¡¯t¡¡notice£»¡¡but¡¡is¡¡necessary¡¡to¡¡distinguish¡¡words£»¡¡sentences£»¡¡and¡¡so¡¡on¡£¡¡Before¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡77¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡55¡¡



we¡¡can¡¡solve¡¡the¡¡whitespace¡¡problem£»¡¡we¡¡need¡¡to¡¡determine¡¡which¡¡part¡¡of¡¡the¡¡application¡¡is¡¡¡¡

not¡¡working¡¡properly¡£¡¡¡¡



Answering¡¡the¡¡Question¡¡of¡¡Responsibility¡¡



The¡¡whitespace¡¡problem¡¡is¡¡an¡¡interesting¡¡one¡£¡¡The¡¡caller¡¡explicitly¡¡put¡¡in¡¡the¡¡extra¡¡spaces£»¡¡but¡¡¡¡

is¡¡the¡¡whitespace¡¡a¡¡bug£»¡¡or¡¡is¡¡it¡¡data¡¡that¡¡is¡¡passed¡¡in¡¡incorrectly£¿¡¡

¡¡¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡can¡¡be¡¡defined¡¡in¡¡the¡¡context¡¡of¡¡reasonable¡¡usage¡£¡¡Imagine¡¡that¡¡you¡¯ve¡¡just¡¡¡¡

bought¡¡a¡¡car¡¡and¡¡you¡¯re¡¡driving¡¡it¡¡on¡¡the¡¡highway¡£¡¡If¡¡the¡¡car¡¡breaks¡¡down£»¡¡the¡¡warranty¡¡will¡¡be¡¡¡¡

in¡¡effect¡¡and¡¡cover¡¡the¡¡costs¡¡of¡¡the¡¡car¡¡repair¡£¡¡Now¡¡imagine¡¡the¡¡situation¡¡where¡¡you¡¡think¡¡you¡¡¡¡

are¡¡one¡¡of¡¡the¡¡Dukes¡¡from¡¡the¡¡Dukes¡¡of¡¡Hazzard¡£¡¡And¡¡being¡¡a¡¡Duke£»¡¡you¡¡drive¡¡your¡¡new¡¡car¡¡as¡¡¡¡

fast¡¡as¡¡you¡¡can¡¡and¡¡then¡¡perform¡¡a¡¡flying¡¡jump¡£¡¡The¡¡car¡¡will¡¡jump¡¡into¡¡air£»¡¡which¡¡looks¡¡quite¡¡¡¡

spectacular£»¡¡but¡¡then¡¡the¡¡car¡¡lands¡£¡¡After¡¡that£»¡¡the¡¡shocks¡¡collapse£»¡¡and¡¡the¡¡car¡¡cannot¡¡be¡¡driven¡¡¡¡

any¡¡further¡£¡¡Even¡¡though¡¡the¡¡car¡¡is¡¡brand¡¡new£»¡¡the¡¡Duke¡¡did¡¡not¡¡use¡¡the¡¡car¡¡as¡¡envisioned¡¡by¡¡¡¡

the¡¡car¡¡manufacturer£»¡¡and¡¡thus¡¡invalidates¡¡the¡¡warranty¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Getting¡¡back¡¡to¡¡the¡¡translation¡¡ponent£»¡¡it¡¡exposes¡¡the¡¡method¡¡TranslateHello£¨£©¡¡and¡¡¡¡

has¡¡responsibilities¡£¡¡And¡¡the¡¡caller¡¡of¡¡TranslateHello£¨£©¡¡has¡¡the¡¡responsibility¡¡of¡¡being¡¡reason

able¡¡about¡¡what¡¡can¡¡be¡¡expected¡¡to¡¡be¡¡translated¡£¡¡So£»¡¡is¡¡whitespace¡¡sent¡¡by¡¡the¡¡caller¡¡reasonable£¿¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡whitespace¡¡is¡¡a¡¡normal¡¡circumstance£»¡¡then¡¡the¡¡failed¡¡test¡¡is¡¡a¡¡bug¡¡in¡¡the¡¡ponent¡£¡¡¡¡

But¡¡if¡¡the¡¡whitespace¡¡is¡¡not¡¡a¡¡normal¡¡circumstance£»¡¡the¡¡caller¡¡is¡¡wrong¡¡and¡¡must¡¡be¡¡fixed¡£¡¡The¡¡¡¡

answer¡¡is¡¡that¡¡the¡¡caller¡¡is¡¡being¡¡reasonable£»¡¡and¡¡the¡¡ponent¡¡is¡¡not¡¡acting¡¡properly¡£¡¡We¡¡¡¡

have¡¡a¡¡bug¡¡in¡¡the¡¡ponent¡¡that¡¡needs¡¡to¡¡be¡¡fixed¡£¡¡How¡¡do¡¡I¡¡know¡¡that¡¡the¡¡caller¡¡is¡¡not¡¡being¡¡¡¡

reasonable£¿¡¡I¡¡made¡¡it¡¡up£»¡¡because¡¡that¡¡is¡¡how¡¡I¡¡expected¡¡the¡¡contract¡¡to¡¡be¡¡implemented¡£¡¡The¡¡¡¡

key¡¡is¡¡in¡¡defining¡¡a¡¡good¡¡contract¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bug¡¡in¡¡the¡¡ponent¡¡relates¡¡to¡¡the¡¡problem¡¡of¡¡how¡¡a¡¡word¡¡is¡¡being¡¡translated¡£¡¡We¡¡¡¡

used¡¡the¡¡pareTo£¨£©¡¡method£»¡¡which¡¡means¡¡that¡¡each¡¡character¡¡is¡¡pared£»¡¡position¡¡for¡¡¡¡

position¡£¡¡The¡¡test¡¡failed¡¡because¡¡the¡¡caller¡¡passed¡¡in¡¡a¡¡string¡¡with¡¡whitespace£»¡¡and¡¡the¡¡ponent¡¡¡¡

expected¡¡no¡¡whitespace¡£¡¡This¡¡bug¡¡is¡¡not¡¡a¡¡surprise£»¡¡because¡¡we¡¡humans¡¡ignore¡¡the¡¡whitespace£»¡¡but¡¡¡¡

puters¡¡cannot¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Before¡¡I¡¡explain¡¡how¡¡to¡¡fix¡¡the¡¡bug£»¡¡I¡¡need¡¡to¡¡take¡¡a¡¡step¡¡back¡¡and¡¡talk¡¡about¡¡what¡¡a¡¡string¡¡¡¡

is¡¡and¡¡how¡¡you¡¡can¡¡figure¡¡out¡¡what¡¡a¡¡string¡¡can¡¡do¡£¡¡¡¡



Investigating¡¡the¡¡String¡¡Type¡¡



String¡¡is¡¡a¡¡class¡¡type£»¡¡and¡¡thus¡¡a¡¡reference¡¡type¡£¡¡¡¡String¡¡is¡¡a¡¡type¡¡that¡¡has¡¡methods¡¡and¡¡proper

ties¡£¡¡Value¡¡types¡¡like¡¡Double¡¡and¡¡Integer¡¡have¡¡methods¡¡and¡¡properties£»¡¡but¡¡String¡¡is¡¡the¡¡first¡¡¡¡

type¡¡you¡¡are¡¡encountering¡¡that¡¡is¡¡a¡¡class¡¡in¡¡its¡¡own¡¡right¡¡£¨Double¡¡and¡¡Integer¡¡are¡¡not¡¡actually¡¡¡¡

classes£©¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡To¡¡investigate¡¡a¡¡type£»¡¡you¡¡can¡¡read¡¡the¡¡documentation£»¡¡or¡¡you¡¡can¡¡use¡¡IntelliSense¡£¡¡Reading¡¡¡¡

the¡¡documentation¡¡is¡¡not¡¡a¡¡bad¡¡idea£»¡¡but¡¡it¡¡is¡¡the¡¡slowest¡¡and¡¡most¡¡cumbersome¡£¡¡IntelliSense¡¡¡¡

is¡¡an¡¡¡¡IDE¡¡approach£»¡¡in¡¡which¡¡a¡¡defined¡¡type¡¯s¡¡methods¡¡and¡¡properties¡¡are¡¡presented¡¡to¡¡you¡¡in¡¡¡¡

an¡¡easy¡­to¡­prehend¡¡manner¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡time¡¡you¡¡use¡¡IntelliSense£»¡¡it¡¡can¡¡be¡¡nerve¡­racking£»¡¡because¡¡it¡¡seems¡¡like¡¡the¡¡IDE¡¡¡¡

has¡¡a¡¡life¡¡of¡¡its¡¡own¡£¡¡There¡¡is¡¡a¡¡convention¡¡on¡¡how¡¡to¡¡use¡¡IntelliSense£»¡¡as¡¡illustrated¡¡in¡¡Figure¡¡3¡­2¡£¡¡¡¡

I¡¡suggest¡¡that¡¡you¡¡take¡¡a¡¡few¡¡moments¡¡to¡¡experiment¡¡with¡¡it¡£¡¡I¡¡also¡¡remend¡¡that¡¡you¡¡keep¡¡¡¡

IntelliSense¡¡active¡¡in¡¡Visual¡¡Basic¡¡Express¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡78¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

56¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡­2¡£¡¡¡¡Using¡¡IntelliSense¡¡on¡¡a¡¡String¡¡typed¡¡variable¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡IntelliSense¡¡works¡¡on¡¡only¡¡identified¡¡variables¡¡that¡¡have¡¡types¡£¡¡In¡¡Figure¡¡3¡­2£»¡¡IntelliSense¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡worked¡¡because¡¡the¡¡IDE¡¡parsed¡¡the¡¡code¡¡and¡¡had¡¡the¡¡ability¡¡to¡¡read¡¡the¡¡metadata¡¡associated¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡the¡¡type¡£¡¡Metadata¡¡is¡¡data¡¡that¡¡describes¡¡your¡¡source¡¡code¡£¡¡Whenever¡¡you¡¡define¡¡a¡¡class£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods¡¡and¡¡properties¡¡are¡¡associated¡¡with¡¡it¡£¡¡The¡¡method¡¡and¡¡property¡¡descriptions¡¡are¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pieces¡¡of¡¡metadata¡¡that¡¡is¡¡displayed¡¡by¡¡IntelliSense¡£¡¡One¡¡of¡¡the¡¡strengths¡¡of¡¡¡¡is¡¡that¡¡all¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡¡have¡¡metadata¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Basis¡¡of¡¡All¡¡Types£º¡¡Object¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡By¡¡default£»¡¡everything¡¡in¡¡¡¡is¡¡an¡¡object¡¡with¡¡a¡¡few¡¡basic¡¡properties¡¡and¡¡methods¡£¡¡Four¡¡basic¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods¡¡are¡¡associated¡¡with¡¡every¡¡object£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Equals£¨£©£º¡¡Verifies¡¡the¡¡equality¡¡of¡¡two¡¡objects¡¡£¨see¡¡Figure¡¡3¡­3£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡GetHashCode£¨£©£º¡¡Retrieves¡¡a¡¡unique¡¡number¡¡describing¡¡the¡¡object¡¡£¨see¡¡Figure¡¡3¡­4£©¡£¡¡Two¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡objects¡¡with¡¡the¡¡same¡¡contents¡¡will¡¡return¡¡the¡¡same¡¡hash¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡GetType£¨£©£º¡¡Retrieves¡¡the¡¡metadata¡¡associated¡¡with¡¡an¡¡object¡¡£¨see¡¡Figure¡¡3¡­5£©¡£¡¡Allows¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡program¡¡to¡¡play¡¡¡°Are¡¡you¡¡a¡¡¡£¡¡¡£¡¡¡£¡¡£¿¡±¡¡to¡¡dynamically¡¡determine¡¡which¡¡methods¡¡and¡¡proper

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ties¡¡a¡¡type¡¡has¡£¡¡It¡¡is¡¡used¡¡by¡¡IntelliSense¡¡to¡¡display¡¡a¡¡list¡¡box¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡ToString£¨£©£º¡¡Converts¡¡the¡¡contents¡¡of¡¡the¡¡type¡¡into¡¡a¡¡string¡¡£¨see¡¡Figure¡¡3¡­6£©¡£¡¡Note¡¡that¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡default¡¡CLR¡¡ToString£¨£©¡¡implementations¡¡work¡¡only¡¡for¡¡value¡¡types¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡79¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡57¡¡



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