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

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

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



GetSheet£¨Of¡¡BaseType£©£¨£©¡¡method£»¡¡as¡¡demonstrated¡¡by¡¡the¡¡following¡¡code¡£¡¡



Friend¡¡Class¡¡Workbook¡¡

¡¡¡¡¡¡¡¡Implements¡¡IWorkbook£»¡¡IDebug¡¡



¡¡¡¡¡¡¡¡Private¡¡_worksheets¡¡As¡¡IDictionary£¨Of¡¡String£»¡¡IWorksheetBase£©¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Dictionary£¨Of¡¡String£»¡¡IWorksheetBase£©£¨£©¡¡



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



¡¡¡¡¡¡¡¡Public¡¡Function¡¡GetSheet£¨Of¡¡BaseType£©£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡As¡¡IWorksheet£¨Of¡¡BaseType£©¡¡Implements¡¡IWorkbook¡£GetSheet¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡SyncLock¡¡_worksheets¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡retval¡¡As¡¡IWorksheet£¨Of¡¡BaseType£©¡¡=¡¡Nothing¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_worksheets¡£ContainsKey£¨identifier£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡retval¡¡=¡¡TryCast£¨_worksheets¡£Item£¨identifier£©£»¡¡¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡IWorksheet£¨Of¡¡BaseType£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡retval¡¡=¡¡New¡¡Worksheet£¨Of¡¡BaseType£©£¨identifier£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_worksheets¡£Add£¨identifier£»¡¡retval£©¡¡

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡324¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡retval¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡SyncLock¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡FunctionEnd¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡shows¡¡that¡¡there¡¡is¡¡still¡¡a¡¡cast£»¡¡but¡¡the¡¡type¡¡cast¡¡is¡¡in¡¡the¡¡method£»¡¡and¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡uses¡¡the¡¡¡¡generics¡¡parameter¡¡declared¡¡at¡¡the¡¡method¡¡level¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡SyncLock¡¡keyword¡¡ensures¡¡that¡¡the¡¡code¡¡in¡¡this¡¡method¡¡is¡¡not¡¡executed¡¡by¡¡more¡¡than¡¡one¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡thread¡¡at¡¡a¡¡time¡£¡¡That¡¡way£»¡¡the¡¡calling¡¡code¡¡always¡¡gets¡¡back¡¡the¡¡correct¡¡sheet¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implementing¡¡the¡¡Server¡¡Spreadsheet¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡let¡¯s¡¡look¡¡at¡¡how¡¡the¡¡workbook¡¡and¡¡worksheet¡¡are¡¡implemented¡£¡¡I¡¡will¡¡explain¡¡only¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡important¡¡pieces£»¡¡but¡¡all¡¡of¡¡the¡¡code¡¡is¡¡available¡¡for¡¡download¡¡from¡¡the¡¡Source/Downloads¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡area¡¡of¡¡the¡¡Apress¡¡web¡¡site¡¡£¨http£º//apress¡££©¡£¡¡The¡¡class¡¡Worksheet£¨Of¡¡BaseType£©¡¡imple

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ments¡¡the¡¡interface¡¡IWorksheet£»¡¡but¡¡does¡¡not¡¡specify¡¡on¡¡which¡¡type¡¡the¡¡worksheet¡¡should¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡based¡£¡¡In¡¡this¡¡application£»¡¡we¡¡also¡¡have¡¡a¡¡class¡¡called¡¡TraderBaseClass¡£¡¡In¡¡most¡¡applications£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡there¡¡is¡¡some¡¡functionality¡¡that¡¡most¡¡classes¡¡will¡¡need¡£¡¡That¡¡mon¡¡functionality¡¡is¡¡what¡¡I¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡call¡¡a¡¡domain¡­specific¡¡base¡¡class¡£¡¡In¡¡the¡¡case¡¡of¡¡TraderBaseClass£»¡¡that¡¡is¡¡the¡¡implementation¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡IDebug¡¡interface¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Using¡¡Lambda¡¡Expressions¡¡in¡¡the¡¡Spreadsheet¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡members¡¡of¡¡Worksheet£¨Of¡¡BaseType£©¡¡are¡¡very¡¡similar¡¡to¡¡the¡¡previously¡¡defined¡¡spread

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sheet¡¡class£»¡¡except¡¡the¡¡declarations¡¡are¡¡lambda¡­ready¡£¡¡Lambda¡­ready¡¡means¡¡that¡¡you¡¡use¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Func£¨Of¡¡£©¡¡type¡¡whenever¡¡you¡¡want¡¡to¡¡declare¡¡a¡¡variable¡¡that¡¡references¡¡a¡¡lambda¡¡expression¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡three¡¡data¡¡members¡¡are¡¡used¡¡to¡¡store¡¡the¡¡state¡¡of¡¡the¡¡cell£»¡¡cell¡¡calculations¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡will¡¡calculate¡¡the¡¡state¡¡of¡¡a¡¡cell£»¡¡and¡¡cell¡¡calculations¡¡that¡¡calculate¡¡the¡¡state¡¡of¡¡cells¡¡for¡¡an¡¡entire¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡column¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Cells¡¡As¡¡Func£¨Of¡¡IWorksheet£¨Of¡¡BaseType£©£»¡¡Integer£»¡¡Integer£»¡¡BaseType£©£¨£»£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡CellState¡¡As¡¡BaseType£¨£»£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡ColCells¡¡As¡¡Func£¨Of¡¡IWorksheet£¨Of¡¡BaseType£©£»¡¡Integer£»¡¡Integer£»¡¡BaseType£©£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡member¡¡CellState¡¡contains¡¡the¡¡state¡¡of¡¡the¡¡worksheet¡¡cell£»¡¡and¡¡its¡¡type¡¡is¡¡BaseType£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡meaning¡¡that¡¡the¡¡type¡¡of¡¡the¡¡worksheet¡¡cell¡¡is¡¡whatever¡¡BaseType¡¡is¡¡declared¡¡as¡£¡¡The¡¡data¡¡members¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Cells¡¡and¡¡ColCells¡¡are¡¡declared¡¡as¡¡lambda¡¡expression¡¡references£»¡¡where¡¡there¡¡are¡¡three¡¡param

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡eters¡¡and¡¡a¡¡return¡¡value¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Before¡¡I¡¡continue¡¡with¡¡the¡¡lambda¡¡expression¡¡explanation£»¡¡I¡¡want¡¡to¡¡shift¡¡focus¡¡to¡¡illustrate¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡problem¡£¡¡We¡¡are¡¡going¡¡to¡¡play¡¡a¡¡game¡¡of¡¡what¡¡animal¡¡am¡¡I£»¡¡where¡¡we¡¯ll¡¡use¡¡lambda¡¡expressions¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡conjunction¡¡with¡¡a¡¡closure¡£¡¡The¡¡idea¡¡is¡¡to¡¡store¡¡the¡¡identifier¡¡of¡¡the¡¡animal¡¡in¡¡a¡¡closure¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡then¡¡return¡¡it¡¡when¡¡it¡¯s¡¡requested¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡325¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡animals£¨1£©¡¡As¡¡Func£¨Of¡¡String£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡animal¡¡As¡¡String¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animal¡¡=¡¡¡¨cow¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animals£¨0£©¡¡=¡¡Function£¨£©¡¡animal¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animal¡¡=¡¡¡¨horse¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animals£¨1£©¡¡=¡¡Function£¨£©¡¡animal¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡callAnimal¡¡In¡¡animals¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£Write¡¡£¨¡¨£¨¡¨¡¡&¡¡callAnimal£¨£©¡¡&¡¡¡¨£©¡¨£©¡¡

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



¡¡¡¡¡¡¡¡¡¡The¡¡example¡¡creates¡¡an¡¡array¡¡of¡¡lambda¡¡expressions¡¡£¨animals£©¡¡where¡¡space¡¡for¡¡two¡¡lambda¡¡¡¡

expressions¡¡is¡¡allocated¡£¡¡The¡¡individual¡¡lambda¡¡expressions¡¡will¡¡reference¡¡a¡¡variable¡¡animal£»¡¡¡¡

which¡¡contains¡¡the¡¡animal¡¡of¡¡the¡¡lambda¡¡expression¡£¡¡In¡¡the¡¡code£»¡¡the¡¡lambda¡¡expressions¡¡are¡¡¡¡

assigned¡¡when¡¡the¡¡state¡¡of¡¡animal¡¡changes¡¡from¡¡cow¡¡to¡¡¡¡horse¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡you¡¡run¡¡the¡¡code£»¡¡and¡¡Console¡£WriteLine£¨£©¡¡generates¡¡its¡¡output¡£¡¡What¡¡do¡¡you¡¡¡¡

think¡¡the¡¡animals¡¡will¡¡be£¿¡¡Do¡¡you¡¡expect¡¡cow¡¡and¡¡¡¡horse£»¡¡respectively£¿¡¡Here¡¯s¡¡the¡¡output£º¡¡



£¨horse£©¡¡£¨horse£©¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡generated¡¡output¡¡is¡¡not¡¡what¡¡you¡¡would¡¡expect¡£¡¡This¡¡demonstrates¡¡that¡¡lambda¡¡expres

sions¡¡are¡¡stateless¡£¡¡Yet¡¡in¡¡earlier¡¡explanations¡¡of¡¡lambda¡¡expressions¡¡and¡¡closures£»¡¡I¡¡said¡¡that¡¡¡¡

variables¡¡can¡¡be¡¡stored¡¡in¡¡lambda¡¡expressions¡£¡¡Since¡¡animal¡¡is¡¡a¡¡value¡¡type£»¡¡you¡¡would¡¡expect¡¡¡¡

two¡¡copies¡¡of¡¡animal£»¡¡right£¿¡¡Wrong¡£¡¡

¡¡¡¡¡¡¡¡¡¡Closures¡¡do¡¡work£»¡¡and¡¡variable¡¡state¡¡is¡¡kept¡¡across¡¡lambda¡¡expressions¡£¡¡The¡¡mistake¡¡in¡¡this¡¡¡¡

code¡¡is¡¡that¡¡the¡¡same¡¡variable¡¡is¡¡referenced¡¡by¡¡two¡¡lambda¡¡expressions¡£¡¡Thus£»¡¡when¡¡the¡¡two¡¡¡¡

lambda¡¡expressions¡¡execute£»¡¡they¡¡will¡¡reference¡¡the¡¡same¡¡animal£»¡¡which¡¡was¡¡last¡¡assigned¡¡to¡¡be¡¡¡¡

a¡¡horse¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡that¡¡solves¡¡the¡¡problem¡¡uses¡¡two¡¡unique¡¡variables£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡animals£¨1£©¡¡As¡¡Func£¨Of¡¡String£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡animal1¡¡=¡¡¡¨cow¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animals£¨0£©¡¡=¡¡Function£¨£©¡¡animal1¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡animal2¡¡=¡¡¡¨horse¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡animals£¨1£©¡¡=¡¡Function£¨£©¡¡animal2¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡callAnimal¡¡In¡¡animals¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨callAnimal£¨£©£©¡¡

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


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡326¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡time¡¡when¡¡you¡¡run¡¡the¡¡code£»¡¡you¡¡get¡¡the¡¡following¡¡output¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cow¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡horse¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Returning¡¡to¡¡the¡¡spreadsheet¡¡example£»¡¡the¡¡problem¡¡is¡¡that¡¡the¡¡behavior¡¡just¡¡described¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡means¡¡this¡¡code¡¡won¡¯t¡¡work£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡myWorksheet¡¡As¡¡IWorksheet£¨Of¡¡Double£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡10¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£AssignCellCalculation£¨row£»¡¡1£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function£¨worksheet£»¡¡cellRow£»¡¡cellCol£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£GetCellState£¨row£»¡¡0£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£Calculate£¨10£»¡¡0£©£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whereas¡¡this¡¡code¡¡would¡¡work£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡myWorksheet¡¡As¡¡IWorksheet£¨Of¡¡Double£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡10¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡temp¡¡=¡¡row¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£AssignCellCalculation£¨row£»¡¡1£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function£¨worksheet£»¡¡cellRow£»¡¡cellCol£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£GetCellState£¨temp£»¡¡0£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡myWorksheet¡£Calculate£¨10£»¡¡0£©£©¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡difference¡¡between¡¡the¡¡two¡¡pieces¡¡of¡¡code¡¡is¡¡that¡¡the¡¡second¡¡one¡¡has¡¡the¡¡lambda¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡expression¡¡that¡¡uses¡¡the¡¡variable¡¡declared¡¡temp¡¡in¡¡the¡¡context¡¡of¡¡a¡¡loop¡£¡¡For¡¡each¡¡iteration¡¡of¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡loop£»¡¡a¡¡new¡¡instance¡¡of¡¡temp¡¡is¡¡allocated£»¡¡and¡¡hence¡¡each¡¡lambda¡¡expression¡¡has¡¡its¡¡own¡¡instance¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡row¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigning¡¡State¡¡Without¡¡Knowing¡¡the¡¡Type¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡using¡¡¡¡generics¡¡types£»¡¡one¡¡of¡¡the¡¡most¡¡mon¡¡problems¡¡occurs¡¡when¡¡you¡¡need¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡work¡¡with¡¡proper¡¡types¡£¡¡In¡¡the¡¡implementation¡¡of¡¡the¡¡IWorksheet¡¡interface£»¡¡it¡¡is¡¡necessary¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implement¡¡the¡¡AssignCellState£¨£©¡¡method¡¡defined¡¡in¡¡the¡¡interface¡¡¡¡IWorksheetSerialize¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡explanation¡¡of¡¡IWorksheetSerialize¡¡is¡¡slightly¡¡plicated¡¡and¡¡relates¡¡to¡¡the¡¡problem¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡loading¡¡an¡¡IWorksheet¡¡without¡¡knowing¡¡the¡¡type¡£¡¡Say¡¡that¡¡you¡¡are¡¡saving¡¡an¡¡¡¡IWorkbook¡¡with¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡multiple¡¡IWorksheet¡¡instances¡£¡¡Each¡¡¡¡IWorksheet¡¡instance¡¡is¡¡a¡¡specific¡¡type¡£¡¡When¡¡you¡¡want¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡load¡¡an¡¡¡¡IWorkbook£»¡¡how¡¡does¡¡the¡¡loader¡¡know¡¡which¡¡types¡¡there¡¡are£¿¡¡The¡¡answer¡¡is¡¡that¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡loader¡¡does¡¡not£»¡¡and¡¡thus¡¡must¡¡first¡¡load¡¡a¡¡general¡¡type£»¡¡and¡¡then¡¡make¡¡a¡¡specific¡¡cast¡£¡¡Take¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡look¡¡at¡¡the¡¡serialization¡¡source¡¡code¡¡in¡¡the¡¡project¡¡Devspace¡£Trader¡£mon¡¡and¡¡the¡¡namespace¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Devspace¡£Trader¡£mon¡£ServerSpreadsheet¡£SerializerImpls£»¡¡available¡¡as¡¡part¡¡of¡¡the¡¡down

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡loadable¡¡code¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡327¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



Public¡¡Interface¡¡IWorksheetSerialize¡¡

¡¡¡¡¡¡¡¡Sub¡¡AssignCellState£¨Of¡¡ValueType£©£¨ByVal¡¡row¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡col¡¡As¡¡Integer£»¡¡ByVal¡¡value¡¡As¡¡ValueType£©¡¡



¡¡¡¡¡¡¡¡Sub¡¡AssignCellState£¨ByVal¡¡row¡¡As¡¡Integer£»¡¡ByVal¡¡col¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡value¡¡As¡¡Object£©¡¡

End¡¡Interface¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡is¡¡that¡¡the¡¡function¡¡passes¡¡in¡¡as¡¡a¡¡parameter¡¡of¡¡one¡¡type¡¡and¡¡is¡¡assigned¡¡¡¡

to¡¡CellState£»¡¡which¡¡is¡¡another¡¡undefined¡¡¡¡generics¡¡type¡£¡¡The¡¡implementation¡¡of¡¡the¡¡¡¡

AssignCellState£¨£©¡¡method¡¡is¡¡as¡¡follows£º¡¡



Public¡¡Sub¡¡AssignCellState£¨ByVal¡¡row¡¡As¡¡Integer£»¡¡ByVal¡¡col¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡ByVal¡¡value¡¡As¡¡Object£©¡¡Implements¡¡IWorksheetSerialize¡£AssignCellState¡¡

¡¡¡¡¡¡¡¡CellState£¨row£»¡¡col£©¡¡=¡¡DirectCast£¨value£»¡¡BaseType£©¡¡

End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡looks¡¡so¡¡simple¡¡and¡¡innocent£»¡¡but¡¡it¡¡is¡¡actually¡¡masking¡¡many¡¡potential¡¡problems¡£¡¡¡¡

Here£»¡¡the¡¡value¡¡of¡¡the¡¡parameter¡¡value¡¡is¡¡of¡¡type¡¡Object¡£¡¡Then¡¡to¡¡convert¡¡the¡¡value¡¡to¡¡type¡¡¡¡

BaseType¡ªbecause¡¡that¡¡is¡¡what¡¡CellState¡¡is¡¡defined¡¡to¡¡be¡ªyou¡¡use¡¡a¡¡cast¡£¡¡For¡¡the¡¡most¡¡part£»¡¡¡¡

this¡¡code¡¡will¡¡work£»¡¡if¡¡value¡¡is¡¡the¡¡correct¡¡type¡£¡¡

¡¡¡¡¡¡¡¡¡¡Consider¡¡the¡¡following¡¡code£»¡¡which¡¡would¡¡generate¡¡an¡¡exception¡£¡¡



Dim¡¡worksheet¡¡As¡¡Worksheet£¨Of¡¡Double£©¡¡=¡¡New¡¡Worksheet£¨Of¡¡Double£©£¨£©¡¡

worksheet¡£Dimension£¨10£»¡¡10£©Dim¡¡buffer¡¡As¡¡String¡¡=¡¡¡¨hello£»¡¡world¡¨¡¡

worksheet¡£AssignCellState£¨1£»¡¡2£»¡¡buffer£©¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡variable¡¡worksheet¡¡is¡¡declared¡¡to¡¡be¡¡of¡¡type¡¡Worksheet£¨Of¡¡Double£©¡£¡¡When¡¡the¡¡method¡¡¡¡

AssignCellState£¨£©¡¡is¡¡called£»¡¡the¡¡cell¡¡1£»¡¡2¡¡is¡¡assigned¡¡to¡¡be¡¡type¡¡String¡£¡¡Calling¡¡AssignCellState£¨£©¡¡is¡¡¡¡

not¡¡a¡¡problem£»¡¡but¡¡in¡¡the¡¡implementation£»¡¡the¡¡assignment¡¡will¡¡fail¡£¡¡You¡¡can¡¯t¡¡willy¡­nilly¡¡assign¡¡¡¡

a¡¡String¡¡to¡¡a¡¡Double¡£¡¡Of¡¡course£»¡¡this¡¡begs¡¡the¡¡question£»¡¡¡°Why¡¡have¡¡a¡¡function¡¡of¡¡type¡¡Object£¿¡±¡¡¡¡

Sometimes¡¡you¡¡can¡¯t¡¡get¡¡around¡¡it£»¡¡and¡¡you¡¡need¡¡to¡¡write¡¡a¡¡general¡¡object¡¡method¡£¡¡The¡¡proper¡¡¡¡

way¡¡to¡¡call¡¡the¡¡method¡¡would¡¡be¡¡as¡¡follows£º¡¡



Dim¡¡worksheet¡¡As¡¡Worksheet£¨Of¡¡Double£©¡¡=¡¡New¡¡Worksheet£¨Of¡¡Double£©£¨£©¡¡

worksheet¡£Dimension£¨10£»¡¡10£©¡¡

Dim¡¡buffer¡¡As¡¡String¡¡=¡¡¡¨hello£»¡¡world¡¨¡¡

worksheet¡£AssignCellState£¨1£»¡¡2£»¡¡Double¡£Parse£¨buffer£©£©¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡shows¡¡that¡¡the¡¡string¡¡buffer¡¡is¡¡parsed¡¡by¡¡the¡¡Double¡£Parse£¨£©¡¡method£»¡¡¡¡

converting¡¡a¡¡string¡¡buffer¡¡into¡¡a¡¡Double¡¡value¡£¡¡Of¡¡course£»¡¡the¡¡conversion¡¡will¡¡fail£»¡¡since¡¡the¡¡string¡¡¡¡

buffer¡¡represents¡¡a¡¡string£»¡¡but¡¡that¡¡is¡¡another¡¡problem¡£¡¡

¡¡¡¡¡¡¡¡¡¡Another¡¡way¡¡of¡¡solving¡¡the¡¡problem¡¡is¡¡to¡¡avoid¡¡the¡¡object¡¡altogether¡¡and¡¡declare¡¡the¡¡method¡¡¡¡

as¡¡being¡¡a¡¡¡¡generics¡¡method¡£¡¡The¡¡advantage¡¡of¡¡the¡¡¡¡generics¡¡method¡¡is¡¡that¡¡you¡¡could¡¡¡¡

execute¡¡in¡¡a¡¡type¡­safe¡¡manner¡¡without¡¡explicitly¡¡forcing¡¡the¡¡user¡¡to¡¡implement¡¡parsing¡¡routines¡£¡¡¡¡

Consider¡¡the¡¡following¡¡modified¡¡¡¡generics¡¡method¡¡declaration¡¡of¡¡AssignCellState£¨£©¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡328¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

306¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡Sub¡¡AssignCellState£¨Of¡¡ValueType£©£¨ByVal¡¡row¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡col¡¡As¡¡Integer£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡value¡¡As¡¡ValueType£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡IWorksheetSerialize¡£AssignCellState¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡GetType£¨BaseType£©¡£IsAssignableFrom£¨GetType£¨ValueType£©£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellState£¨row£»¡¡col£©¡¡=¡¡DirectCast£¨DirectCast£¨value£»¡¡Object£©£»¡¡BaseType£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ElseIf¡¡TypeOf¡¡£¨value£©¡¡Is¡¡String¡¡And¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡GetType£¨Double£©¡£IsAssignableFrom£¨GetType£¨BaseType£©£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡obj¡¡As¡¡Object¡¡=¡¡DirectCast£¨value£»¡¡Object£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡dValue¡¡As¡¡Double¡¡=¡¡Double¡£Parse£¨CStr£¨obj£©£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡objDValue¡¡As¡¡Object¡¡=¡¡CType£¨dValue£»¡¡Object£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellState£¨row£»¡¡col£©¡¡=¡¡DirectCast£¨objDValue£»¡¡BaseType£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡New¡¡InvalidCastException£¨¡¨Could¡¡not¡¡perform¡¡conversion¡¨£©¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡cell¡¡state¡¡to¡¡be¡¡assigned¡¡is¡¡a¡¡¡¡generics¡¡parameter¡¡and¡¡defined¡¡to¡¡be¡¡the¡¡type¡¡ValueType¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡only¡¡guess¡¡what¡¡ValueType¡¡is£»¡¡it¡¡is¡¡determined¡¡when¡¡the¡¡method¡¡AssignCellState£¨Of¡¡£©£¨£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡called¡£¡¡For¡¡example£»¡¡suppose¡¡this¡¡method¡¡call¡¡is¡¡made£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡buffer¡¡As¡¡String¡¡=¡¡¡¨hello£»¡¡world¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡worksheet¡£AssignCellState£¨1£»¡¡2£»¡¡buffer£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡type¡¡for¡¡ValueType¡¡will¡¡be¡¡String£»¡¡even¡¡though¡¡you¡¡have¡¡not¡¡explicitly¡¡specified¡¡it¡£¡¡One¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡things¡¡that¡¡is¡¡possible¡¡with¡¡¡¡generics¡¡methods¡¡is¡¡that¡¡types¡¡can¡¡be¡¡deduced¡¡implic

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡itly¡£¡¡The¡¡following¡¡would¡¡be¡¡an¡¡explicit¡¡usage¡¡of¡¡AssignCellState£¨Of¡¡£©£¨£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡buffer¡¡As¡¡String¡¡=¡¡¡¨hello£»¡¡world¡¨¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡worksheet¡£AssignCellState£¨Of¡¡String£©£¨1£»¡¡2£»¡¡buffer£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Knowing¡¡that¡¡ValueType¡¡is¡¡String£»¡¡AssignCellState£¨Of¡¡£©£¨£©¡¡will¡¡then¡¡first¡¡check¡¡if¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ValueType¡¡can¡¡be¡¡assigned¡¡to¡¡¡¡BaseType£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡GetType£¨BaseType£©¡£IsAssignableFrom£¨GetType£¨ValueType£©£©¡¡Then¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡code¡¡is¡¡rather¡¡clever£»¡¡because¡¡it¡¡uses¡¡what¡¡is¡¡known¡¡as¡¡¡¡reflection¡¡to¡¡determine¡¡if¡¡one¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡can¡¡be¡¡assigned¡¡to¡¡another¡¡type¡£¡¡Essentially£»¡¡it¡¡asks¡¡if¡¡it¡¡is¡¡OK¡¡via¡¡a¡¡cast¡¡to¡¡assign¡¡value¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellState¡£¡¡You¡¡could¡¡try¡¡to¡¡do¡¡this¡¡without¡¡the¡¡If¡¡statement£»¡¡but¡¡then¡¡you¡¡risk¡¡an¡¡unnecessary¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡exception¡£¡¡If¡¡it¡¡is¡¡permissible¡¡to¡¡assign£»¡¡then¡¡the¡¡assignment¡¡is¡¡done¡¡via¡¡a¡¡two¡­step¡¡casting£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellState£¨row£»¡¡col£©¡¡=¡¡DirectCast£¨DirectCast£¨value£»¡¡Object£©£»¡¡BaseType£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Here£»¡¡you¡¡first¡¡convert¡¡the¡¡type¡¡to¡¡an¡¡Object£»¡¡and¡¡then¡¡convert¡¡the¡¡type¡¡to¡¡BaseType£»¡¡which¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡happens¡¡to¡¡be¡¡the¡¡type¡¡that¡¡the¡¡spreadsheet¡¡is¡¡declared¡¡as¡£¡¡It¡¡is¡¡absolutely¡¡imperative¡¡that¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cast¡¡to¡¡the¡¡object¡¡is¡¡added£»¡¡otherwise£»¡¡the¡¡Visual¡¡Basic¡¡piler¡¡will¡¡plain¡¡that¡¡the¡¡cast¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡possible¡£¡¡

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