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

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

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


¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡ticket¡¡As¡¡New¡¡Ticket£¨¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡DateTime£¨¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨dateSplit£¨0£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨dateSplit£¨1£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨dateSplit£¨2£©£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Integer£¨£©¡¡£û¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨1£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨2£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨3£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨4£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨5£©£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨6£©£©£ý£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡£Parse£¨splitUpText£¨7£©£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_tickets¡£Add£¨ticket£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Loop¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡¡¨¡¨¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡examine¡¡how¡¡the¡¡implementation¡¡works¡£¡¡



Borrowing¡¡Code¡¡to¡¡Solve¡¡Another¡¡Problem¡¡



The¡¡borrowed¡¡code¡¡is¡¡the¡¡implementation¡¡of¡¡the¡¡¡¡Process£¨£©¡¡method£»¡¡shown¡¡here¡¡in¡¡abbrevi

ated¡¡form£º¡¡



¡¡¡¡¡¡¡¡Public¡¡Function¡¡Process£¨ByVal¡¡input¡¡As¡¡String£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡String¡¡Implements¡¡IProcessor¡£Process¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡reader¡¡As¡¡TextReader¡¡=¡¡New¡¡StringReader£¨input£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Do¡¡While¡¡reader¡£Peek¡¡¡¡¡­1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡splitUpText¡¡As¡¡String£¨£©¡¡=¡¡reader¡£ReadLine¡£Split£¨New¡¡Char£¨£©¡¡£û¡¨¡¡¡¨c£ý£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡dateSplit¡¡As¡¡String£¨£©¡¡=¡¡splitUpText£¨0£©¡£Split£¨New¡¡Char£¨£©¡¡£û¡¨¡£¡¨c£ý£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡ticket¡¡As¡¡New¡¡Ticket£¨¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡£¡¡¡£¡¡¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Loop¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡¡¨¡¨¡¡

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡424¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

402¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Other¡¡than¡¡the¡¡new¡¡_tickets¡£Add£¨ticket£©¡¡line£»¡¡the¡¡code¡¡is¡¡identical¡¡to¡¡the¡¡Text2Binary¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Process£¨£©¡¡implementation¡£¡¡The¡¡bolded¡¡code¡¡adds¡¡the¡¡lottery¡¡draw¡¡to¡¡the¡¡list¡¡of¡¡drawn¡¡numbers¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡the¡¡tickets¡¡have¡¡been¡¡instantiated£»¡¡they¡¡can¡¡be¡¡added¡¡to¡¡the¡¡list¡¡of¡¡tickets¡¡that¡¡will¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡queried¡¡and¡¡searched¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CODE¡¡REUSE¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡borrowed¡¡code¡¡demonstrates¡¡code¡¡reuse¡¡through¡¡copy¡¡and¡¡paste¡£¡¡Realize¡¡that¡¡code¡¡that¡¡was¡¡used¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡process¡¡binary¡¡objects¡¡is¡¡now¡¡being¡¡used¡¡in¡¡a¡¡pletely¡¡new¡¡context¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡copied¡¡and¡¡pasted¡¡code¡¡reuses¡¡classes¡¡and¡¡functionality¡¡from¡¡another¡¡problem¡¡context¡£¡¡You¡¡copied¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡pasted¡¡the¡¡functionality¡¡to¡¡parse¡¡and¡¡instantiate¡¡the¡¡Ticket¡¡type£»¡¡but¡¡reused¡¡the¡¡Ticket¡¡type¡¡itself¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡we¡¡worked¡¡on¡¡the¡¡code¡¡for¡¡the¡¡Ticket¡¡type£»¡¡you¡¡probably¡¡had¡¡no¡¡idea¡¡that¡¡we¡¡would¡¡reuse¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡same¡¡code¡£¡¡In¡¡fact£»¡¡even¡¡I¡¡had¡¡no¡¡idea¡¡that¡¡we¡¡would¡¡reuse¡¡the¡¡code¡£¡¡I¡¡find¡¡this¡¡happens¡¡a¡¡lot¡¡in¡¡my¡¡projects¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡So¡¡how¡¡does¡¡this¡¡work£¿¡¡I¡¡write¡¡code¡¡to¡¡fulfill¡¡two¡¡criteria£º¡¡minimal¡¡to¡¡solve¡¡the¡¡task¡¡and¡¡general¡¡enough¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡not¡¡restrict¡¡further¡¡usage¡£¡¡So¡¡I¡¡don¡¯t¡¡actually¡¡design¡¡for¡¡code¡¡reuse£»¡¡and¡¡I¡¡don¡¯t¡¡have¡¡code¡¡reuse¡¡on¡¡my¡¡mind¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡I¡¡have¡¡on¡¡my¡¡mind¡¡is¡¡to¡¡design¡¡code¡¡in¡¡such¡¡a¡¡way¡¡that¡¡it¡¡could¡¡potentially¡¡be¡¡reused¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡look¡¡at¡¡that¡¡code¡¡that¡¡could¡¡have¡¡solved¡¡the¡¡frequency¡¡problem¡¡previously¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡splitUpText£¨£©¡¡as¡¡String¡¡=¡¡lineOfText¡£Split£¨New¡¡Char£¨£©¡¡£û¡¡¡¨¡¡¡¨c¡¡£ý£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡frequency£¨Integer¡£Parse£¨splitUpText£¨0£©£©£©¡¡£«=¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡frequency£¨Integer¡£Parse£¨splitUpText£¨1£©£©£©¡¡£«=¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Is¡¡this¡¡code¡¡efficient£¿¡¡No£»¡¡even¡¡though¡¡the¡¡lines¡¡of¡¡code¡¡are¡¡minimal£»¡¡the¡¡code¡¡itself¡¡is¡¡not¡¡efficient¡£¡¡If¡¡I¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡wanted¡¡to¡¡use¡¡the¡¡same¡¡code¡¡to¡¡perform¡¡another¡¡frequency¡¡analysis£»¡¡which¡¡could¡¡happen£»¡¡I¡¡would¡¡need¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡copy¡¡and¡¡paste¡¡yet¡¡again£»¡¡and¡¡thus¡¡the¡¡code¡¡is¡¡not¡¡efficient¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Is¡¡this¡¡code¡¡general¡¡enough¡¡to¡¡be¡¡used¡¡in¡¡another¡¡context£¿¡¡Absolutely¡¡not£»¡¡because¡¡to¡¡reuse¡¡the¡¡code£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡would¡¡need¡¡to¡¡copy¡¡and¡¡paste¡¡it£»¡¡and¡¡do¡¡some¡¡slight¡¡alterations¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡is¡¡an¡¡excellent¡¡example¡¡of¡¡code¡¡that¡¡can¡¡be¡¡written¡¡very¡¡quickly¡¡and¡¡is¡¡very¡¡effectively¡¡used¡¡by¡¡copying¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡pasting¡¡it¡¡everywhere¡£¡¡You¡¡are¡¡productive¡¡and¡¡can¡¡solve¡¡a¡¡problem¡¡quickly£»¡¡but¡¡it¡¡cannot¡¡be¡¡easily¡¡extended¡¡or¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡maintained¡£¡¡Imagine¡¡finding¡¡a¡¡bug¡¡and¡¡having¡¡copied¡¡and¡¡pasted¡¡the¡¡code¡¡ten¡¡times¡£¡¡That¡¡would¡¡mean¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡would¡¡need¡¡to¡¡find¡¡the¡¡ten¡¡different¡¡locations¡¡and¡¡see¡¡if¡¡the¡¡bug¡¡exists¡¡in¡¡those¡¡different¡¡locations¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Using¡¡LINQ¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡find¡¡the¡¡frequency¡¡of¡¡a¡¡specific¡¡number£»¡¡you¡¡don¡¯t¡¡need¡¡to¡¡use¡¡LINQ¡£¡¡In¡¡fact£»¡¡LINQ¡¡can¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡always¡¡be¡¡avoided¡¡by¡¡using¡¡Visual¡¡Basic¡¡code¡£¡¡So¡¡then¡¡why¡¡use¡¡LINQ£¿¡¡The¡¡reason¡¡is¡¡that¡¡LINQ¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡makes¡¡it¡¡easier¡¡for¡¡you¡¡to¡¡write¡¡plicated¡¡search¡¡queries¡¡that¡¡are¡¡agnostic¡¡of¡¡the¡¡source¡£¡¡An¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡example¡¡is¡¡two¡¡versions¡¡of¡¡the¡¡code¡¡used¡¡to¡¡solve¡¡the¡¡frequency¡¡problem£º¡¡one¡¡that¡¡is¡¡not¡¡reus

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡able¡¡and¡¡one¡¡that¡¡is¡¡reusable¡£¡¡The¡¡code¡¡that¡¡is¡¡not¡¡reusable¡¡is¡¡the¡¡query¡¡without¡¡LINQ£»¡¡and¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡reusable¡¡code¡¡is¡¡the¡¡query¡¡with¡¡LINQ¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡So¡¡let¡¯s¡¡look¡¡at¡¡the¡¡frequency¡¡code¡¡that¡¡is¡¡not¡¡reusable¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡425¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡403¡¡



Function¡¡FrequencyOfANumberNotReusable£¨ByVal¡¡numberToSearch¡¡As¡¡Integer£©¡¡_¡¡

¡¡¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡Dim¡¡runningTotal¡¡As¡¡Integer¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡For¡¡Each¡¡aTicket¡¡as¡¡Ticket¡¡in¡¡_tickets¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡aTicket¡£Numbers£¨0£©¡¡=¡¡numberToSearch¡¡OrElse¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aTicket¡£Numbers£¨1£©¡¡=¡¡numberToSearch¡¡OrElse¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aTicket¡£Numbers£¨2£©¡¡=¡¡numberToSearch¡¡OrElse¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aTicket¡£Numbers£¨3£©¡¡=¡¡numberToSearch¡¡OrElse¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aTicket¡£Numbers£¨4£©¡¡=¡¡numberToSearch¡¡OrElse¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aTicket¡£Numbers£¨5£©¡¡=¡¡numberToSearch¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡runningTotal¡¡£«=¡¡1¡¡

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

¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡Return¡¡runningTotal¡¡

End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡Notice¡¡the¡¡similarity¡¡of¡¡the¡¡code¡¡to¡¡the¡¡code¡¡presented¡¡in¡¡Chapter¡¡9¡£¡¡The¡¡problem¡¡with¡¡this¡¡¡¡

code¡¡is¡¡that¡¡you¡¡are¡¡iterating¡¡and¡¡solving¡¡a¡¡particular¡¡problem¡£¡¡The¡¡code¡¡cannot¡¡be¡¡easily¡¡adapted¡¡¡¡

to¡¡solving¡¡another¡¡problem¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡reusable¡¡code¡¡is¡¡in¡¡the¡¡form¡¡of¡¡a¡¡LINQ¡¡expression£º¡¡



¡¡¡¡¡¡¡¡Private¡¡Function¡¡FrequencyOfANumber£¨ByVal¡¡numberToSearch¡¡As¡¡Integer£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡query¡¡=¡¡From¡¡ticket¡¡In¡¡_tickets¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Where¡¡ticket¡£Numbers£¨0£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨1£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨2£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨3£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨4£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨5£©¡¡=¡¡numberToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select¡¡ticket¡£Numbers¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡query¡£Count£¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡The¡¡LINQ¡¡expression¡¡uses¡¡many¡¡constructs¡¡similar¡¡to¡¡a¡¡SQL¡¡SELECT¡¡statement¡£¡¡Here¡¡are¡¡¡¡

the¡¡basic¡¡rules¡¡of¡¡LINQ£º¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡All¡¡LINQ¡¡queries¡¡must¡¡have¡¡a¡¡data¡¡source¡¡£¨From£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡All¡¡LINQ¡¡queries¡¡must¡¡have¡¡a¡¡filter¡¡£¨Where£©£»¡¡however£»¡¡if¡¡the¡¡filter¡¡does¡¡not¡¡exist£»¡¡an¡¡auto

¡¡¡¡¡¡¡¡¡¡¡¡¡¡matic¡¡include¡­everything¡¡filter¡¡is¡¡implied¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡All¡¡LINQ¡¡queries¡¡must¡¡have¡¡a¡¡resulting¡¡dataset¡¡creator¡¡£¨Select£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡To¡¡execute¡¡a¡¡LINQ¡¡expression£»¡¡you¡¡need¡¡a¡¡data¡¡source¡£¡¡The¡¡data¡¡source¡¡could¡¡be¡¡an¡¡object¡¡¡¡

list£»¡¡an¡¡XML¡¡document£»¡¡or¡¡even¡¡a¡¡relational¡¡database¡¡table¡£¡¡In¡¡the¡¡example£»¡¡the¡¡data¡¡source¡¡is¡¡¡¡

an¡¡object¡¡list¡¡and¡¡is¡¡defined¡¡using¡¡the¡¡From¡¡statement£º¡¡



From¡¡ticket¡¡In¡¡_tickets¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡426¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

404¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Looking¡¡at¡¡the¡¡From¡¡statement£»¡¡you¡¡could¡¡get¡¡the¡¡idea¡¡that¡¡it¡¡is¡¡a¡¡¡¡For¡¡Each¡¡statement¡¡without¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡types¡£¡¡Indeed£»¡¡the¡¡iteration¡¡happens¡¡only¡¡when¡¡you¡¡actually¡¡try¡¡to¡¡use¡¡the¡¡result¡¡of¡¡the¡¡LINQ¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡query¡£¡¡The¡¡From¡¡statement¡¡is¡¡saying¡¡to¡¡iterate¡¡the¡¡data¡¡source¡¡and¡¡assign¡¡each¡¡element¡¡£¨a¡¡Ticket£©¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡variable¡¡ticket¡£¡¡Note£»¡¡however£»¡¡that¡¡there¡¡is¡¡no¡¡explicit¡¡type¡¡information£»¡¡which¡¡is¡¡one¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡strengths¡¡of¡¡LINQ¡ªyou¡¡have¡¡the¡¡ability¡¡to¡¡easily¡¡slice¡¡and¡¡dice¡¡data¡¡to¡¡suit¡¡your¡¡needs¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡retrieve¡¡each¡¡item£»¡¡you¡¡want¡¡to¡¡verify¡¡whether¡¡the¡¡item¡¡matches¡¡your¡¡needs¡£¡¡If¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡look¡¡at¡¡the¡¡code¡¡that¡¡isn¡¯t¡¡reusable£»¡¡you¡¯ll¡¡see¡¡that¡¡it¡¡checks¡¡this¡¡with¡¡an¡¡If¡¡statement¡£¡¡In¡¡LINQ£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡use¡¡the¡¡Where¡¡statement£»¡¡which¡¡is¡¡similar¡¡to¡¡its¡¡SQL¡¡equivalent¡£¡¡With¡¡the¡¡Where¡¡statement£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡test¡¡to¡¡see¡¡if¡¡the¡¡item¡¡matches¡¡your¡¡criteria¡£¡¡In¡¡our¡¡case£»¡¡we¡¡check¡¡each¡¡number¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Ticket¡¡instance¡¡to¡¡see¡¡if¡¡it¡¡matches¡¡the¡¡number¡¡we¡¯re¡¡currently¡¡seeking¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡Where¡¡returns¡¡True£»¡¡we¡¡have¡¡a¡¡match¡¡and¡¡we¡¡will¡¡want¡¡to¡¡do¡¡something¡£¡¡In¡¡the¡¡code¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡isn¡¯t¡¡reusable£»¡¡that¡¡means¡¡incrementing¡¡the¡¡runningTotal¡¡integer¡£¡¡In¡¡LINQ£»¡¡the¡¡aim¡¡is¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡filter¡¡the¡¡data¡¡source¡¡£¨_tickets¡¡in¡¡our¡¡case£©£»¡¡and¡¡thus¡¡the¡¡¡¡Select¡¡statement¡¡is¡¡used¡¡to¡¡create¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡new¡¡result¡¡set¡¡of¡¡drawn¡¡numbers¡£¡¡This¡¡result¡¡set¡¡contains¡¡all¡¡of¡¡the¡¡draws¡¡with¡¡the¡¡number¡¡we¡¯re¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡looking¡¡for¡¡£¨numberToSearch£©£»¡¡and¡¡if¡¡the¡¡draws¡¡are¡¡counted£»¡¡we¡¡can¡¡get¡¡the¡¡frequency¡¡of¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number£»¡¡which¡¡we¡¡then¡¡return¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡look¡¡at¡¡the¡¡LINQ¡¡that¡¡could¡¡be¡¡used¡¡to¡¡find¡¡the¡¡frequency¡¡of¡¡two¡¡numbers¡¡being¡¡drawn¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function¡¡FrequencyOfTwoNumbers£¨ByVal¡¡number1ToSearch¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡number2ToSearch¡¡As¡¡Integer£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡query¡¡=¡¡From¡¡ticket2¡¡In¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨From¡¡ticket¡¡In¡¡_tickets¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Where¡¡ticket¡£Numbers£¨0£©¡¡=¡¡number1ToSearch¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨1£©¡¡=¡¡number1ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨2£©¡¡=¡¡number1ToSearch¡¡¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨3£©¡¡=¡¡number1ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨4£©¡¡=¡¡number1ToSearch¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket¡£Numbers£¨5£©¡¡=¡¡number1ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select¡¡ticket£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Where¡¡ticket2¡£Numbers£¨0£©¡¡=¡¡number2ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket2¡£Numbers£¨1£©¡¡=¡¡number2ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket2¡£Numbers£¨2£©¡¡=¡¡number2ToSearch¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket2¡£Numbers£¨3£©¡¡=¡¡number2ToSearch¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket2¡£Numbers£¨4£©¡¡=¡¡number2ToSearch¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Or¡¡ticket2¡£Numbers£¨5£©¡¡=¡¡number2ToSearch¡¡_¡¡

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