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

µÚ82²¿·Ö

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

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

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




¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Managed£»¡¡which¡¡references¡¡the¡¡variable¡¡_managed¡£¡¡The¡¡idea¡¡behind¡¡this¡¡class¡¡is¡¡to¡¡assign¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡object¡¡in¡¡the¡¡constructor¡¡that¡¡can¡¡be¡¡referenced¡¡using¡¡the¡¡property¡£¡¡The¡¡class¡¡Container¡¡does¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡know¡¡what¡¡the¡¡variable¡¡_managed¡¡does¡¡or¡¡its¡¡capabilities¡£¡¡Container¡¡does¡¡not¡¡care£»¡¡because¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Container¡¡is¡¡acting¡¡like¡¡a¡¡basket¡¡that¡¡holds¡¡an¡¡instance¡¡of¡¡whatever¡¡is¡¡given¡¡to¡¡it¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Container¡¡class¡¡could¡¡be¡¡used¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡container¡¡As¡¡Container¡¡=¡¡New¡¡Container£¨New¡¡MyType£¨£©£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TryCast£¨container¡£Managed£»¡¡MyType£©¡£Method£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡Container¡¡is¡¡instantiated£»¡¡the¡¡_managed¡¡data¡¡member¡¡is¡¡assigned¡¡an¡¡instance¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyType¡£¡¡MyType¡¡is¡¡a¡¡type¡¡that¡¡is¡¡used¡¡for¡¡illustrative¡¡purposes¡¡and¡¡has¡¡a¡¡single¡¡method£»¡¡Method£¨£©¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡retrieve¡¡and¡¡use¡¡the¡¡managed¡¡type£»¡¡the¡¡property¡¡Managed¡¡is¡¡referenced¡£¡¡However£»¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡method¡¡Method£¨£©¡¡cannot¡¡be¡¡called¡¡directly¡¡because¡¡the¡¡property¡¡Managed¡¡is¡¡of¡¡type¡¡Object£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡thus¡¡you¡¡need¡¡to¡¡cast¡¡the¡¡property¡¡to¡¡type¡¡MyType¡¡so¡¡that¡¡the¡¡call¡¡to¡¡Method£¨£©¡¡is¡¡legal¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡cast¡¡using¡¡TryCast¡¡will¡¡result¡¡in¡¡a¡¡valid¡¡instance¡¡of¡¡MyType¡¡or¡¡a¡¡Nothing¡¡value£»¡¡resulting¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡a¡¡null¡¡object¡¡reference¡¡exception¡£¡¡Here¡¯s¡¡a¡¡safe¡¡way¡¡of¡¡using¡¡the¡¡Managed¡¡property£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡container¡¡As¡¡Container¡¡=¡¡New¡¡Container£¨New¡¡MyType£¨£©£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡TypeOf¡¡container¡£Managed¡¡Is¡¡MyType¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TryCast£¨container¡£Managed£»¡¡MyType£©¡£Method£¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡is¡¡the¡¡addition¡¡of¡¡the¡¡If¡¡block¡¡to¡¡test¡¡whether¡¡the¡¡container¡¡references¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡MyType¡£¡¡Not¡¡shown¡¡is¡¡what¡¡the¡¡code¡¡should¡¡do¡¡if¡¡the¡¡property¡¡Managed¡¡is¡¡not¡¡MyType¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fact¡¡that¡¡you¡¡need¡¡to¡¡verify¡¡that¡¡the¡¡container¡¡references¡¡the¡¡correct¡¡type£»¡¡and¡¡you¡¡also¡¡need¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡think¡¡of¡¡an¡¡alternative¡¡plan¡¡if¡¡the¡¡type¡¡is¡¡incorrect£»¡¡adds¡¡quite¡¡a¡¡bit¡¡of¡¡code¡£¡¡It¡¯s¡¡like¡¡that¡¡sentence¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡about¡¡the¡¡duck¡ªsure£»¡¡you¡¡get¡¡the¡¡general¡¡idea¡¡of¡¡what¡¡is¡¡being¡¡said£»¡¡but¡¡are¡¡you¡¡100£¥¡¡sure¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡meaning£¿¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡309¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡287¡¡



¡¡¡¡¡¡¡¡¡¡Now¡¡look¡¡at¡¡the¡¡following¡¡code£»¡¡which¡¡implements¡¡a¡¡container¡¡using¡¡¡¡generics¡£¡¡



¡¡¡¡¡¡¡¡Public¡¡Class¡¡GenericsContainer£¨Of¡¡ManagedType£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_managed¡¡As¡¡ManagedType¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡toManage¡¡As¡¡ManagedType£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_managed¡¡=¡¡toManage¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Managed£¨£©¡¡As¡¡ManagedType¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_managed¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡

¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡write¡¡code¡¡that¡¡uses¡¡¡¡generics¡¡and¡¡code¡¡that¡¡provides¡¡types¡¡based¡¡on¡¡¡¡¡¡

generics¡£¡¡The¡¡definition¡¡of¡¡GenericsContainer¡¡demonstrates¡¡code¡¡that¡¡provides¡¡types¡¡based¡¡on¡¡¡¡

¡¡generics¡£¡¡You¡¯ll¡¡see¡¡the¡¡code¡¡that¡¡uses¡¡¡¡generics¡¡next¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡parameters¡¡are¡¡associated¡¡with¡¡types£»¡¡such¡¡as¡¡classes¡¡and¡¡interfaces£»¡¡or¡¡with¡¡¡¡

methods¡£¡¡In¡¡the¡¡case¡¡of¡¡GenericsContainer£»¡¡the¡¡¡¡generics¡¡parameter¡¡ManagedType¡¡is¡¡a¡¡¡¡

placeholder¡¡for¡¡an¡¡actual¡¡type¡£¡¡



¡öNote¡¡¡¡monly£»¡¡developers¡¡use¡¡the¡¡notation¡¡of¡¡a¡¡single¡¡letter¡¡when¡¡defining¡¡a¡¡¡¡generics¡¡parameter¡£¡¡¡¡

I¡¡am¡¡not¡¡a¡¡fan¡¡of¡¡that¡¡notation£»¡¡because¡¡it¡¡tells¡¡me¡¡nothing£»¡¡especially¡¡when¡¡there¡¡are¡¡multiple¡¡parameters¡£¡¡I¡¡¡¡

remend¡¡using¡¡an¡¡identifier¡¡that¡¡describes¡¡what¡¡the¡¡parameter¡¡does£»¡¡appended¡¡with¡¡the¡¡word¡¡Type£»¡¡to¡¡¡¡

indicate¡¡a¡¡¡¡generics¡¡parameter¡¡is¡¡being¡¡defined¡£¡¡



¡¡¡¡¡¡¡¡¡¡With¡¡GenericsContainer£»¡¡ManagedType¡¡is¡¡used¡¡as¡¡an¡¡identifier¡¡in¡¡the¡¡place¡¡of¡¡the¡¡identifier¡¡¡¡

object¡¡in¡¡the¡¡type¡¡Container¡£¡¡This¡¡is¡¡a¡¡rule¡¡of¡¡thumb¡£¡¡Whenever¡¡you¡¡find¡¡yourself¡¡using¡¡an¡¡object¡¡¡¡

generically¡¡to¡¡define¡¡a¡¡type£»¡¡you¡¡probably¡¡could¡¡use¡¡¡¡generics¡£¡¡Think¡¡of¡¡¡¡generics¡¡¡¡

types¡¡as¡¡general¡¡things¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡code¡¡demonstrates¡¡how¡¡to¡¡use¡¡GenericsContainer¡¡with¡¡MyType¡£¡¡



Dim¡¡container¡¡As¡¡GenericsContainer£¨Of¡¡MyType£©¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡New¡¡GenericsContainer£¨Of¡¡MyType£©£¨New¡¡MyType£¨£©£©¡¡

container¡£Managed¡£Method£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡When¡¡instantiating¡¡GenericsContainer£»¡¡notice¡¡how¡¡the¡¡identifier¡¡ManagedType¡¡must¡¡be¡¡¡¡

replaced¡¡with¡¡an¡¡identifier¡¡that¡¡represents¡¡an¡¡already¡¡existing¡¡type¡£¡¡This¡¡identifier¡¡replacement¡¡¡¡

results¡¡in¡¡a¡¡new¡¡and¡¡unique¡¡type¡¡£¨also¡¡called¡¡a¡¡concrete¡¡type£©¡£¡¡The¡¡advantage¡¡of¡¡¡¡generics¡¡is¡¡¡¡

that¡¡when¡¡you¡¡provide¡¡a¡¡concrete¡¡type£»¡¡you¡¡don¡¯t¡¡need¡¡to¡¡check¡¡to¡¡make¡¡sure¡¡that¡¡everything¡¡is¡¡¡¡

correct¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡¡¡runtime¡¡will¡¡generate¡¡a¡¡type¡¡that¡¡has¡¡an¡¡intent¡¡similar¡¡to¡¡the¡¡following¡¡code¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡310¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡GenericsContainerMyType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡_managed¡¡As¡¡MyType¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡toManage¡¡As¡¡MyType£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_managed¡¡=¡¡toManage¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Managed£¨£©¡¡As¡¡MyType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_managed¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡piles¡¡a¡¡¡¡generics¡¡type¡¡as¡¡an¡¡inplete¡¡type¡£¡¡When¡¡the¡¡inplete¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡is¡¡concretized£»¡¡¡¡creates¡¡a¡¡brand¡­new¡¡type£»¡¡and¡¡does¡¡this¡¡without¡¡requiring¡¡the¡¡devel

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oper¡¡to¡¡do¡¡anything¡¡in¡¡particular¡£¡¡This¡¡means¡¡that¡¡if¡¡you¡¡use¡¡GenericsContainer¡¡with¡¡15¡¡different¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types£»¡¡¡¡will¡¡generate¡¡15¡¡definitions¡¡of¡¡GenericsContainer¡¡while¡¡the¡¡program¡¡is¡¡executing¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ABSTRACTION¡¡AND¡¡¡¡GENERICS¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡¡¡generics£»¡¡you¡¡can¡¡verify¡¡and¡¡ensure¡¡that¡¡everything¡¡is¡¡being¡¡said¡¡properly£»¡¡with¡¡the¡¡cost¡¡being¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡plexity¡£¡¡Consider¡¡the¡¡sentence¡¡that¡¡clearly¡¡described¡¡the¡¡duck¡£¡¡To¡¡produce¡¡it£»¡¡you¡¡need¡¡to¡¡have¡¡learned¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡additional¡¡rules¡¡regarding¡¡grammar¡£¡¡When¡¡using¡¡¡¡generics£»¡¡you¡¡need¡¡to¡¡master¡¡creating¡¡abstractions¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡are¡¡an¡¡abstraction¡£¡¡Just¡¡as¡¡interfaces¡¡are¡¡an¡¡abstraction¡¡of¡¡classes£»¡¡¡¡generics¡¡are¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡abstraction¡¡above¡¡interfaces¡£¡¡Interfaces¡¡define¡¡an¡¡intention£»¡¡and¡¡¡¡generics¡¡define¡¡an¡¡abstract¡¡implemen

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tation¡¡of¡¡an¡¡intention¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡is¡¡challenging¡¡with¡¡¡¡generics¡¡is¡¡getting¡¡your¡¡thoughts¡¡together¡¡into¡¡an¡¡abstract¡¡intention¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡£¡¡It¡¡is¡¡like¡¡writing¡¡a¡¡document¡ªyou¡¡write¡¡it¡¡once£»¡¡read¡¡it¡¡over£»¡¡rewrite¡¡it£»¡¡read¡¡it¡¡over£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rewrite¡¡it¡¡again¡£¡¡With¡¡¡¡generics£»¡¡you¡¡are¡¡gathering¡¡thoughts¡¡together¡¡into¡¡a¡¡general¡¡plan¡¡of¡¡action¡£¡¡This¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡why¡¡some¡¡people¡¡are¡¡pletely¡¡confused¡¡and¡¡don¡¯t¡¡understand¡¡¡¡generics¡£¡¡Writing¡¡your¡¡own¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡code¡¡requires¡¡some¡¡forethought¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Theory¡¡of¡¡a¡¡Server¡­Side¡¡Spreadsheet¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡example¡¡in¡¡this¡¡chapter¡¡is¡¡a¡¡spreadsheet¡¡for¡¡security¡¡traders¡£¡¡When¡¡you¡¡trade¡¡securities¡ª¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whether¡¡they¡¡are¡¡equities£»¡¡bonds£»¡¡options£»¡¡or¡¡futures¡ªyou¡¡will¡¡be¡¡confronted¡¡with¡¡information¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡overload¡£¡¡You¡¡might¡¡have¡¡seen¡¡pictures¡¡of¡¡traders¡¡with¡¡desks¡¡full¡¡of¡¡desktop¡¡monitors¡£¡¡A¡¡trader¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡might¡¡have¡¡seven¡¡to¡¡eight¡¡monitors¡¡displaying¡¡various¡¡bits¡¡of¡¡information¡£¡¡A¡¡trader¡¡is¡¡a¡¡very¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡specialized¡¡type¡¡of¡¡domain¡¡that¡¡requires¡¡its¡¡own¡¡ways¡¡of¡¡processing¡¡information¡£¡¡One¡¡aspect¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡makes¡¡writing¡¡applications¡¡for¡¡traders¡¡difficult¡¡is¡¡that¡¡the¡¡nature¡¡of¡¡the¡¡data¡¡constantly¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡changes£»¡¡and¡¡types¡¡get¡¡more¡¡in¡¡the¡¡way¡¡than¡¡they¡¡help¡£¡¡As¡¡a¡¡result£»¡¡traders¡¡adore¡¡spreadsheets¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Spreadsheets¡¡are¡¡useful¡¡because¡¡they¡¡can¡¡process¡¡large¡¡amounts¡¡of¡¡information¡¡in¡¡a¡¡relatively¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ad¡¡hoc¡¡manner¡£¡¡However£»¡¡one¡¡downside¡¡to¡¡spreadsheets¡¡is¡¡that¡¡the¡¡processing¡¡time¡¡can¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡311¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡289¡¡



dramatically¡¡increase¡¡due¡¡to¡¡the¡¡constant¡¡pushing¡¡and¡¡pulling¡¡of¡¡the¡¡data¡¡to¡¡and¡¡from¡¡the¡¡¡¡

spreadsheet¡£¡¡To¡¡speed¡¡up¡¡processing£»¡¡we¡¡will¡¡define¡¡and¡¡implement¡¡a¡¡spreadsheet¡¡that¡¡has¡¡the¡¡¡¡

advantages¡¡of¡¡a¡¡traditional¡¡client¡­side¡¡spreadsheet¡£¡¡



¡öNote¡¡¡¡The¡¡theory¡¡and¡¡solution¡¡presented¡¡here¡¡are¡¡specific¡¡to¡¡the¡¡domain¡¡of¡¡trading£»¡¡where¡¡the¡¡cost¡¡of¡¡hard

ware¡¡is¡¡well¡¡worth¡¡the¡¡ability¡¡to¡¡trade¡¡properly¡£¡¡Therefore£»¡¡specific¡¡design¡¡aspects¡¡assume¡¡that¡¡you¡¡have¡¡the¡¡¡¡

latest¡¡and¡¡greatest¡¡hardware¡£¡¡



¡¡¡¡¡¡¡¡¡¡An¡¡initial¡¡attempt¡¡at¡¡a¡¡spreadsheet¡¡would¡¡be¡¡the¡¡following¡¡code¡¡that¡¡uses¡¡¡¡generics£º¡¡



Class¡¡Spreadsheet¡¡

¡¡¡¡¡¡¡¡Public¡¡Cells¡¡As¡¡Func£¨Of¡¡Object£©£¨£»£©¡¡

¡¡¡¡¡¡¡¡Public¡¡State¡¡As¡¡Object£¨£»£©¡¡



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Cells¡¡=¡¡New¡¡Func£¨Of¡¡Object£©£¨10£»¡¡10£©¡¡£û£ý¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡State¡¡=¡¡New¡¡Object£¨10£»¡¡10£©¡¡£û£ý¡¡

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



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Execute£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡col¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨1£©¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨0£©¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨col¡¡&¡¡¡¨¡¡¡¨¡¡&¡¡¡¨¡¡¡¨¡¡&¡¡row£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡Cells£¨row£»¡¡col£©¡¡IsNot¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡State£¨row£»¡¡col£©¡¡=¡¡Cells£¨row£»¡¡col£©£¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡



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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡sample¡¡spreadsheet¡¡is¡¡defined¡¡using¡¡the¡¡data¡¡members¡¡Cells¡¡and¡¡State¡£¡¡Both¡¡data¡¡¡¡

members¡¡are¡¡arrays¡¡with¡¡two¡¡dimensions¡£¡¡The¡¡first¡¡dimension¡¡represents¡¡the¡¡rows£»¡¡and¡¡the¡¡¡¡

second¡¡dimension¡¡represents¡¡the¡¡columns¡£¡¡You¡¡could¡¡define¡¡as¡¡many¡¡dimensions¡¡as¡¡you¡¡wish£»¡¡¡¡

but¡¡for¡¡the¡¡scope¡¡of¡¡the¡¡server¡¡spreadsheet£»¡¡we¡¡take¡¡a¡¡two¡­dimensional¡¡approach¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡¡¡Execute£¨£©¡¡method¡¡goes¡¡through¡¡the¡¡individual¡¡rows¡¡and¡¡columns¡¡of¡¡the¡¡Cells¡¡data¡¡¡¡

member£»¡¡calculates¡¡the¡¡state¡¡of¡¡the¡¡cell£»¡¡and¡¡assigns¡¡the¡¡state¡¡to¡¡the¡¡State¡¡data¡¡member¡£¡¡The¡¡¡¡

data¡¡member¡¡Cells¡¡represents¡¡a¡¡function¡¡that¡¡is¡¡executed¡¡to¡¡generate¡¡the¡¡result¡¡of¡¡a¡¡particul

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

Äã¿ÉÄÜϲ»¶µÄ