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

µÚ141ÕÂ

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ141ÕÂ

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

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡






¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡Destroy£¨£©¡¡As¡¡String¡¡Implements¡¡IExtendedProcessor¡£Destroy¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡builder¡¡As¡¡New¡¡StringBuilder£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡c1¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡c1¡¡=¡¡1¡¡To¡¡46¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨¡¨Number¡¡£¨¡¨¡¡&¡¡c1¡¡&¡¡¡¨£©¡¡Found¡¡£¨¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡foundCount¡¡As¡¡Integer¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨£¨£¨foundCount¡¡£«¡¡FrequencyOfANumber£¨c1£©£©¡¡&¡¡¡¨£©¡¨¡¡&¡¡ChrW£¨10£©£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡c1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡builder¡£ToString£¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡423¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡Public¡¡Function¡¡Initialize£¨£©¡¡As¡¡String¡¡Implements¡¡IExtendedProcessor¡£Initialize¡¡

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

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



¡¡¡¡¡¡¡¡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£¨¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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

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

Äã¿ÉÄÜϲ»¶µÄ