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

µÚ13²¿·Ö

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

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

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 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¡¡1

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

Äã¿ÉÄÜϲ»¶µÄ