ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ54²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_constant¡¡=¡¡&H25¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_runningTotal¡¡=¡¡&H11¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡AppendSuper£¨ByVal¡¡superHashCode¡¡As¡¡Integer£©¡¡As¡¡HashCodeAutomater¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_runningTotal¡¡=¡¡£¨£¨_runningTotal¡¡*¡¡_constant£©¡¡£«¡¡superHashCode£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡Append£¨ByVal¡¡obj¡¡As¡¡Object£©¡¡As¡¡HashCodeAutomater¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨obj¡¡Is¡¡Nothing£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£_runningTotal¡¡=¡¡£¨Me¡£_runningTotal¡¡*¡¡Me¡£_constant£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡Not¡¡obj¡£GetType¡£IsArray¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£_runningTotal¡¡=¡¡£¨£¨Me¡£_runningTotal¡¡*¡¡Me¡£_constant£©¡¡£«¡¡obj¡£GetHashCode£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Long£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Long£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Integer£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Integer£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Short£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Short£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Char£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Char£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡303¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡1¡¡0¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡O¡¡U¡¡T¡¡¡¡P¡¡E¡¡R¡¡S¡¡IS¡¡T¡¡E¡¡N¡¡CE¡¡281¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Byte£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Byte£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Double£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Double£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Single£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Single£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡obj¡¡Is¡¡Boolean£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Boolean£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨DirectCast£¨obj£»¡¡Object£¨£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡Function¡¡Append£¨ByVal¡¡array¡¡As¡¡Long£¨£©£©¡¡As¡¡HashCodeAutomater¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨array¡¡Is¡¡Nothing£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£_runningTotal¡¡=¡¡£¨Me¡£_runningTotal¡¡*¡¡Me¡£_constant£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡i¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡i¡¡=¡¡0¡¡To¡¡array¡£Length¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Append£¨array£¨i£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡i¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡Function¡¡Append£¨ByVal¡¡value¡¡As¡¡Long£©¡¡As¡¡HashCodeAutomater¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£_runningTotal¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨£¨Me¡£_runningTotal¡¡*¡¡Me¡£_constant£©¡¡£«¡¡CInt£¨£¨value¡¡Xor¡¡£¨value¡¡¡·¡·¡¡&H20£©£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡Function¡¡ToHashCode£¨£©¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_runningTotal¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡different¡¡implementations¡¡of¡¡the¡¡Append£¨£©¡¡method¡¡belong¡¡to¡¡a¡¡single¡¡grouping¡¡for¡¡a¡¡¡¡
single¡¡data¡¡type£»¡¡Long¡£¡¡For¡¡example£»¡¡there¡¡is¡¡an¡¡Append£¨£©¡¡method¡¡that¡¡accepts¡¡a¡¡¡¡Long¡¡and¡¡a¡¡¡¡Long¡¡¡¡
array¡£¡¡The¡¡full¡¡implementation¡¡of¡¡HashCodeAutomater¡¡would¡¡have¡¡an¡¡Append£¨£©¡¡method¡¡for¡¡Short¡¡¡¡
and¡¡Short¡¡array£»¡¡and¡¡all¡¡the¡¡other¡¡data¡¡types¡£¡¡There¡¡is¡¡no¡¡specific¡¡group¡¡implementation¡¡for¡¡the¡¡¡¡
String¡¡type£»¡¡because¡¡it¡¡is¡¡treated¡¡like¡¡an¡¡object¡¡that¡¡has¡¡its¡¡own¡¡hash¡code¡¡calculation¡¡¡¡
implementation¡£¡¡
¡¡¡¡¡¡¡¡¡¡Notice¡¡in¡¡the¡¡implementations¡¡of¡¡the¡¡Append£¨£©¡¡methods¡¡how¡¡a¡¡calculation¡¡is¡¡performed¡¡¡¡
and¡¡then¡¡added¡¡to¡¡the¡¡data¡¡member¡¡_runningTotal¡£¡¡The¡¡return¡¡value¡¡is¡¡a¡¡Me¡¡reference£»¡¡so¡¡that¡¡¡¡
the¡¡methods¡¡can¡¡be¡¡chained¡¡together¡£¡¡This¡¡allows¡¡a¡¡client¡¡to¡¡use¡¡the¡¡HashCodeAutomater¡¡class£»¡¡¡¡
as¡¡the¡¡following¡¡GetHashCode£¨£©¡¡implementation¡¡demonstrates£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡304¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
282¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡0¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡P¡¡E¡¡R¡¡S¡¡IS¡¡TE¡¡N¡¡CE¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡HashcodeExample¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡buffer¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New¡¡£¨ByVal¡¡val¡¡As¡¡Integer£»¡¡ByVal¡¡buf¡¡As¡¡String£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡=¡¡val¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡buf¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡GetHashCode£¨£©¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡New¡¡HashCodeAutomater£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£Append£¨value£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£Append£¨buffer£©¡£toHashCode£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡implementation¡¡of¡¡HashcodeExample¡¡has¡¡two¡¡data¡¡members£º¡¡value¡¡and¡¡buffer¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡two¡¡data¡¡members¡¡make¡¡up¡¡the¡¡class¡¯s¡¡state¡£¡¡Not¡¡all¡¡data¡¡members¡¡are¡¡used¡¡when¡¡calculating¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡class¡¡instance¡¯s¡¡hash¡code¡¡value¡£¡¡For¡¡example£»¡¡if¡¡HashcodeExample¡¡had¡¡a¡¡data¡¡member¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡referenced¡¡a¡¡database¡¡connection£»¡¡it¡¡should¡¡not¡¡be¡¡used¡¡when¡¡calculating¡¡the¡¡hash¡¡code£»¡¡because¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡database¡¡connection¡¡is¡¡the¡¡type¡¡used¡¡to¡¡get¡¡the¡¡state¡¡and¡¡does¡¡not¡¡influence¡¡the¡¡state¡ªit¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡means¡¡to¡¡an¡¡end¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implementing¡¡Equals£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡the¡¡GetHashCode£¨£©¡¡method¡¡has¡¡been¡¡implemented£»¡¡the¡¡¡¡Equals£¨£©¡¡method¡¡can¡¡be¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implemented£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡Equals£¨ByVal¡¡obj¡¡As¡¡Object£©¡¡As¡¡Boolean¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡TypeOf£¨obj£©¡¡Is¡¡HashCodeExample¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡obj¡£GetHashCode£¨£©¡¡=¡¡Me¡£GetHashCode£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡True¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Because¡¡the¡¡rule¡¡for¡¡GetHashCode£¨£©¡¡is¡¡that¡¡two¡¡object¡¡instances¡¡with¡¡identical¡¡hash¡code¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡values¡¡must¡¡return¡¡the¡¡same¡¡value£»¡¡it¡¡makes¡¡sense¡¡to¡¡implement¡¡Equals£¨£©¡¡using¡¡GetHashCode£¨£©¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡what¡¡started¡¡out¡¡as¡¡a¡¡good¡¡idea¡¡turns¡¡out¡¡to¡¡be¡¡a¡¡bad¡¡idea£»¡¡as¡¡the¡¡following¡¡illustrates£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡s1¡¡As¡¡String¡¡=¡¡¡¨Hello¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡s2¡¡As¡¡String¡¡=¡¡¡¨World¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡x1¡¡As¡¡Integer¡¡=¡¡17¡¡*¡¡17¡¡£«¡¡s1¡£GetHashCode£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡x2¡¡As¡¡Integer¡¡=¡¡17¡¡*¡¡17¡¡£«¡¡s2¡£GetHashCode£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡h1¡¡As¡¡HashCodeExample¡¡=¡¡New¡¡HashCodeExample¡¡£¨x2¡¡*¡¡37£»¡¡s1£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡h2¡¡As¡¡HashCodeExample¡¡=¡¡New¡¡HashCodeExample¡¡£¨x1¡¡¡¡*¡¡37£»¡¡s2£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡305¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡1¡¡0¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡O¡¡U¡¡T¡¡¡¡P¡¡E¡¡R¡¡S¡¡IS¡¡T¡¡E¡¡N¡¡CE¡¡283¡¡
¡¡¡¡¡¡¡¡Dim¡¡ht¡¡As¡¡Hashtable¡¡=¡¡New¡¡Hashtable£¨£©¡¡
¡¡¡¡¡¡¡¡ht¡£Add£¨h1£»¡¡Nothing£©¡¡
¡¡¡¡¡¡¡¡ht¡£Add£¨h2£»¡¡Nothing£©¡¡
¡¡¡¡¡¡¡¡¡¡This¡¡shows¡¡that¡¡having¡¡two¡¡objects¡¡with¡¡pletely¡¡different¡¡states¡¡results¡¡in¡¡the¡¡same¡¡¡¡
hash¡code¡¡value¡¡and¡¡generates¡¡an¡¡exception¡¡because¡¡Equals£¨£©¡¡has¡¡been¡¡implemented¡¡incor
rectly¡£¡¡In¡¡the¡¡implementation¡¡of¡¡Hashtable£»¡¡when¡¡an¡¡added¡¡object¡¡collides¡¡with¡¡another¡¡already¡¡¡¡
existing¡¡object£»¡¡an¡¡equality¡¡test¡¡is¡¡made¡£¡¡If¡¡the¡¡equality¡¡test¡¡returns¡¡True£»¡¡then¡¡the¡¡exception¡¡¡¡
is¡¡generated¡¡because¡¡Hashtable¡¡does¡¡not¡¡allow¡¡you¡¡to¡¡add¡¡an¡¡object¡¡with¡¡the¡¡same¡¡state¡¡as¡¡¡¡
another¡¡object¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡solution¡¡is¡¡not¡¡to¡¡fix¡¡the¡¡GetHashCode£¨£©¡¡method£»¡¡but¡¡rather¡¡to¡¡modify¡¡the¡¡Equals£¨£©¡¡method£º¡¡
¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡Equals£¨ByVal¡¡obj¡¡As¡¡Object£©¡¡As¡¡Boolean¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡TypeOf¡¡£¨obj£©¡¡Is¡¡HashcodeExample¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡obj¡£GetHashCode£¨£©¡¡¡¡Me¡£GetHashCode£¨£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡toTest¡¡As¡¡HashcodeExample¡¡=¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡DirectCast£¨obj£»¡¡HashcodeExample£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡toTest¡£value¡¡=¡¡Me¡£value¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡toTest¡£buffer¡¡=¡¡Me¡£buffer¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡True¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡logic¡¡of¡¡the¡¡modified¡¡Equals£¨£©¡¡method¡¡is¡¡to¡¡first¡¡test¡¡if¡¡both¡¡types¡¡are¡¡identical¡£¡¡If¡¡not£»¡¡¡¡
then¡¡False¡¡is¡¡returned¡£¡¡Next£»¡¡test¡¡if¡¡GetHashCode£¨£©¡¡returns¡¡unequal¡¡values¡£¡¡GetHashCode£¨£©¡¡will¡¡¡¡
always¡¡return¡¡different¡¡values¡¡for¡¡objects¡¡that¡¡have¡¡different¡¡data¡¡members¡£¡¡If¡¡the¡¡hash¡code¡¡¡¡
values¡¡are¡¡equal£»¡¡then¡¡es¡¡the¡¡hard¡¡work¡¡of¡¡individually¡¡testing¡¡each¡¡data¡¡member¡¡for¡¡equality¡£¡¡¡¡
The¡¡hard¡¡work¡¡is¡¡delegated¡¡as¡¡the¡¡last¡¡step£»¡¡because¡¡any¡¡object¡¡that¡¡reaches¡¡that¡¡point¡¡will¡¡¡¡
probably¡¡be¡¡identical£»¡¡but¡¡you¡¡need¡¡to¡¡be¡¡100£¥¡¡certain¡£¡¡
The¡¡Important¡¡Stuff¡¡to¡¡Remember¡¡
In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡how¡¡to¡¡process¡¡a¡¡stream¡¡of¡¡data¡¡using¡¡the¡¡console¡£¡¡Here¡¡are¡¡the¡¡¡¡
main¡¡items¡¡to¡¡remember£º¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡When¡¡data¡¡is¡¡moved¡¡from¡¡one¡¡medium¡¡to¡¡another£»¡¡it¡¡is¡¡streamed¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡There¡¡are¡¡two¡¡major¡¡types¡¡of¡¡streams£º¡¡text¡¡and¡¡binary¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡Text¡¡streams¡¡are¡¡universal¡¡and¡¡can¡¡be¡¡read¡¡by¡¡all¡¡puters¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡306¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
284¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡0¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡P¡¡E¡¡R¡¡S¡¡IS¡¡TE¡¡N¡¡CE¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Binary¡¡streams¡¡are¡¡specific¡¡to¡¡the¡¡program¡¡and¡¡sometimes¡¡to¡¡the¡¡processor¡£¡¡Imagine¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡situation¡¡of¡¡having¡¡to¡¡decipher¡¡a¡¡C£«£«¡¡data¡¡stream¡¡generated¡¡by¡¡a¡¡PowerPC¡¡chip¡£¡¡Most¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡likely£»¡¡the¡¡numbers¡¡that¡¡you¡¡read¡¡will¡¡be¡¡wrong¡¡because¡¡of¡¡the¡¡way¡¡that¡¡Intel¡¡or¡¡AMD¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡chips¡¡store¡¡their¡¡numbers¡£¡¡Generally¡¡speaking£»¡¡with¡¡binary¡¡streams£»¡¡you¡¡will¡¡be¡¡conversing¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡two¡¡¡¡implementations¡£¡¡If¡¡not£»¡¡use¡¡text¡¡streams¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡streaming¡¡data£»¡¡it¡¡is¡¡best¡¡to¡¡customize¡¡as¡¡little¡¡as¡¡possible¡£¡¡Doing¡¡so¡¡will¡¡pli
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cate¡¡your¡¡program£»¡¡and¡¡potentially¡¡introduce¡¡errors¡¡where¡¡none¡¡should¡¡exist¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡It¡¡is¡¡important¡¡that¡¡you¡¡understand¡¡the¡¡concept¡¡of¡¡marshaling¡¡and¡¡the¡¡fact¡¡that¡¡each¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡medium¡¡will¡¡have¡¡a¡¡different¡¡representation¡¡of¡¡the¡¡type¡£¡¡A¡¡large¡¡part¡¡of¡¡your¡¡programming¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡day¡¡will¡¡involve¡¡moving¡¡data¡¡from¡¡one¡¡stream¡¡to¡¡another¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Some¡¡Things¡¡for¡¡You¡¡to¡¡Do¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡are¡¡some¡¡exercises¡¡to¡¡apply¡¡what¡¡you¡¡learned¡¡in¡¡this¡¡chapter¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡In¡¡the¡¡implementation¡¡of¡¡TextProcessor£»¡¡the¡¡display¡¡help¡¡routine¡¡was¡¡not¡¡very¡¡helpful¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Fix¡¡the¡¡implementation¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡There¡¡were¡¡no¡¡testing¡¡routines¡¡for¡¡TextProcessor¡£¡¡Devise¡¡some¡¡realistic¡¡tests¡£¡¡By¡¡realistic¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tests£»¡¡I¡¡mean¡¡tests¡¡that¡¡don¡¯t¡¡just¡¡focus¡¡on¡¡the¡¡class¡¡library¡¡and¡¡consider¡¡the¡¡application¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tested¡£¡¡Focus¡¡on¡¡plete¡¡application¡¡tests¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Having¡¡implemented¡¡the¡¡display¡¡help¡¡routine£»¡¡think¡¡about¡¡whether¡¡or¡¡not¡¡the¡¡imple
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mentation¡¡is¡¡correct¡£¡¡The¡¡class¡¡Bootstrap¡¡is¡¡a¡¡general¡¡class¡¡that¡¡uses¡¡an¡¡¡¡IProcessor¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instance£»¡¡which¡¡means¡¡that¡¡different¡¡console¡¡applications¡¡will¡¡process¡¡different¡¡data¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thus£»¡¡writing¡¡a¡¡general¡¡help¡¡output¡¡might¡¡work£»¡¡but¡¡it¡¡will¡¡not¡¡help¡¡in¡¡resolving¡¡problems¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Fix¡¡the¡¡console¡¡application¡¡TextProcessor¡¡and¡¡¡¡ReaderWriter¡¡project¡¡so¡¡that¡¡the¡¡help¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡message¡¡is¡¡both¡¡specific¡¡and¡¡general¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡In¡¡the¡¡Bootstrap¡¡class£»¡¡when¡¡the¡¡output¡¡was¡¡redirected¡¡to¡¡a¡¡file¡¡£¨as¡¡indicated¡¡by¡¡the¡¡¡¡¡out¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡argument£©£»¡¡there¡¡was¡¡no¡¡check¡¡on¡¡whether¡¡or¡¡not¡¡the¡¡file¡¡exists¡£¡¡Extend¡¡the¡¡Bootstrap¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡to¡¡include¡¡an¡¡additional¡¡mand¡line¡¡argument¡¡that¡¡verifies¡¡if¡¡it¡¡is¡¡fine¡¡to¡¡overwrite¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡output¡¡file¡¡if¡¡it¡¡exists¡£¡¡If¡¡an¡¡output¡¡file¡¡does¡¡exist¡¡and¡¡there¡¡is¡¡no¡¡explicit¡¡overwriting£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generate¡¡an¡¡error¡¡and¡¡stop¡¡processing¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡The¡¡code¡¡in¡¡the¡¡final¡¡solution¡¡for¡¡IProcessor¡£Process£¨£©¡¡has¡¡been¡¡identified¡¡as¡¡being¡¡hard¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡maintain¡¡because¡¡the¡¡code¡¡to¡¡check¡¡for¡¡duplicate¡¡dates¡¡is¡¡scattered¡¡throughout¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡method¡£¡¡Rewrite¡¡the¡¡method¡¡implementation¡¡so¡¡that¡¡the¡¡code¡¡is¡¡logical¡¡and¡¡maintainable¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡307¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
C¡¡¡¡H¡¡¡¡A¡¡¡¡P¡¡¡¡T¡¡¡¡E¡¡¡¡R¡¡¡¡¡¡¡¡¡¡1¡¡¡¡1¡¡
¡ö¡¡¡ö¡¡¡ö¡¡
Learning¡¡About¡¡¡¡Generics¡¡
Chapter¡¡9¡¡explained¡¡how¡¡to¡¡use¡¡lists£»¡¡delegates£»¡¡and¡¡lambda¡¡expressions¡£¡¡In¡¡that¡¡chapter£»¡¡you¡¡¡¡
also¡¡saw¡¡an¡¡example¡¡of¡¡¡¡generics¡¡when¡¡using¡¡lists¡¡to¡¡manage¡¡a¡¡collection¡¡of¡¡object¡¡instances¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡main¡¡focus¡¡of¡¡this¡¡chapter¡¡is¡¡¡¡generics¡¡and¡¡how¡¡to¡¡use¡¡them¡¡in¡¡a¡¡black¡¡box¡¡context¡¡¡¡
£¨the¡¡code¡¡doesn¡¯t¡¡know¡¡the¡¡specifics¡¡of¡¡the¡¡¡¡generics¡¡parameter¡¡types£©¡£¡¡The¡¡secondary¡¡¡¡
focus¡¡is¡¡an¡¡implementation¡¡of¡¡lambda¡¡expressions¡¡using¡¡a¡¡spreadsheet¡£¡¡The¡¡idea¡¡is¡¡to¡¡get¡¡you¡¡¡¡
well¡¡versed¡¡in¡¡¡¡generics¡¡and¡¡lambda¡¡expressions£»¡¡which¡¡you¡¡will¡¡likely¡¡use¡¡in¡¡your¡¡own¡¡¡¡
production¡¡code£»¡¡so¡¡that¡¡that¡¡there¡¡will¡¡be¡¡no¡¡surprises¡¡in¡¡your¡¡projects¡£¡¡¡¡
Why¡¡Use¡¡¡¡Generics£¿¡¡
Here¡¯s¡¡a¡¡surprise¡¡for¡¡you£º¡¡there¡¡is¡¡no¡¡imperative¡¡need¡¡for¡¡¡¡generics£»¡¡so¡¡you¡¡could¡¡skip¡¡this¡¡¡¡
chapter¡¡and¡¡read¡¡the¡¡next¡¡one£»¡¡right£¿¡¡Wrong¡£¡¡I¡¡could¡¡just¡¡as¡¡easily¡¡have¡¡said¡¡there¡¡is¡¡no¡¡need¡¡for¡¡¡¡
Visual¡¡Basic¡¡properties£»¡¡nor¡¡any¡¡other¡¡Visual¡¡Basic¡¡construct¡¡that¡¡enriches¡¡your¡¡programming¡¡¡¡
abilities¡£¡¡The¡¡reasons¡¡we¡¡have¡¡Visual¡¡Basic¡¡properties¡¡and¡¡¡¡generics¡¡are¡¡programming¡¡¡¡
elegance¡¡and¡¡expressiveness¡£¡¡
¡¡¡¡¡¡¡¡¡¡To¡¡understand¡¡what¡¡I¡¡am¡¡trying¡¡to¡¡get¡¡at£»¡¡consider¡¡this¡¡sentence£º¡¡
¡¡¡¡¡¡¡¡¡¡Ducks¡¡walk¡¡flat¡¡feet¡¡quack¡¡loud¡¡
¡¡¡¡¡¡¡¡¡¡Reading¡¡the¡¡sentence£»¡¡you¡¡get¡¡an¡¡idea¡¡of¡¡what¡¡is¡¡being¡¡said£»¡¡but¡¡you¡¡are¡¡not¡¡pletely¡¡¡¡
sure¡£¡¡Visual¡¡Basic¡¡without¡¡¡¡generics¡¡is¡¡like¡¡this¡¡sentence£»¡¡in¡¡that¡¡you¡¡express¡¡your¡¡ideas¡¡in¡¡¡¡
code£»¡¡but¡¡some¡¡things¡¡are¡¡not¡¡as¡¡clear¡¡as¡¡you¡¡would¡¡like¡£¡¡
¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡with¡¡¡¡generics¡¡is¡¡like¡¡this¡¡sentence£º¡¡
¡¡¡¡¡¡¡¡¡¡Ducks¡¡walk¡¡in¡¡a¡¡funny¡¡manner¡¡due¡¡to¡¡their¡¡flat¡¡feet£»¡¡and¡¡when¡¡they¡¡quack£»¡¡it¡¡is¡¡very¡¡
¡¡¡¡¡¡¡¡¡¡loud¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡sentence¡¡is¡¡clearer¡¡and¡¡uses¡¡more¡¡words¡¡to¡¡describe¡¡the¡¡same¡¡thing¡£¡¡The¡¡reason¡¡we¡¡¡¡
talk¡¡using¡¡a¡¡more¡¡sophisticated¡¡language¡¡is¡¡that¡¡we¡¡want¡¡to¡¡explain¡¡concepts¡¡and¡¡be¡¡under
stood¡£¡¡If¡¡you¡¡accept¡¡that£»¡¡then¡¡you¡¡can¡¡accept¡¡why¡¡there¡¡is¡¡a¡¡need¡¡and¡¡context¡¡for¡¡¡¡generics¡£¡¡¡¡
And¡¡if¡¡not£»¡¡feel¡¡free¡¡to¡¡skip¡¡this¡¡chapter¡¡and¡¡read¡¡the¡¡next¡¡one¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡An¡¡example¡¡that¡¡illustrates¡¡how¡¡using¡¡¡¡generics¡¡makes¡¡your¡¡code¡¡clearer£»¡¡as¡¡well¡¡as¡¡¡¡
more¡¡concise£»¡¡than¡¡code¡¡that¡¡does¡¡not¡¡use¡¡¡¡generics¡¡is¡¡a¡¡container¡£¡¡A¡¡¡¡container¡¡is¡¡a¡¡type¡¡¡¡
that¡¡is¡¡used¡¡to¡¡manage¡¡other¡¡types£»¡¡lists¡¡and¡¡collections¡¡are¡¡examples¡¡of¡¡containers¡£¡¡To¡¡keep¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡285¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡308¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
286¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡things¡¡simple£»¡¡let¡¯s¡¡look¡¡at¡¡a¡¡container¡¡that¡¡manages¡¡a¡¡single¡¡reference¡£¡¡The¡¡following¡¡is¡¡the¡¡less¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡concise¡¡version¡¡that¡¡uses¡¡the¡¡Object¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡Container¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_managed¡¡As¡¡Object¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡toManage¡¡As¡¡Object£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_managed¡¡=¡¡toManage¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Managed£¨£©¡¡As¡¡Object¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_managed¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡code£»¡¡the¡¡class¡¡Container¡¡has¡¡a¡¡constructor¡¡with¡¡a¡¡parameter¡¡and¡¡a¡¡single¡¡property£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Managed£»¡¡which¡¡references¡¡the¡¡variable¡¡_managed¡£¡¡The¡¡idea¡¡
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡