VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ87²¿·Ö
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
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