ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ59²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡possible¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡But¡¡suppose¡¡that¡¡the¡¡type¡¡cannot¡¡be¡¡directly¡¡assigned¡£¡¡Let¡¯s¡¡say¡¡that¡¡value¡¡references¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡String¡¡that¡¡contains¡¡a¡¡number¡£¡¡Then£»¡¡using¡¡reflection£»¡¡you¡¡can¡¡verify¡¡what¡¡ValueType¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡BaseType¡¡are¡¡and¡¡perform¡¡the¡¡conversion¡¡yourself£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡329¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡307¡¡
ElseIf¡¡TypeOf¡¡£¨value£©¡¡Is¡¡String¡¡And¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡GetType£¨Double£©¡£IsAssignableFrom£¨GetType£¨BaseType£©£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡part¡¡of¡¡the¡¡If¡¡statement¡¡asks¡¡if¡¡value¡¡is¡¡of¡¡type¡¡String¡£¡¡The¡¡second¡¡part¡¡asks¡¡if¡¡Double¡¡¡¡
can¡¡be¡¡assigned¡¡to¡¡BaseType¡£¡¡If¡¡both¡¡are¡¡valid£»¡¡it¡¡means¡¡the¡¡input¡¡is¡¡a¡¡String¡¡and¡¡the¡¡spread
sheet¡¡type¡¡is¡¡a¡¡Double£»¡¡so¡¡to¡¡convert£»¡¡you¡¡only¡¡need¡¡to¡¡call¡¡Double¡£Parse£¨£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡Providing¡¡this¡¡automatic¡¡conversion¡¡functionality¡¡for¡¡the¡¡caller¡¡of¡¡the¡¡code¡¡does¡¡not¡¡save¡¡¡¡
code£»¡¡but¡¡it¡¡centralizes¡¡it¡¡and¡¡makes¡¡it¡¡general¡£¡¡The¡¡caller¡¡will¡¡not¡¡generally¡¡need¡¡to¡¡worry¡¡about¡¡¡¡
the¡¡most¡¡mon¡¡conversions£»¡¡as¡¡they¡¡will¡¡happen¡¡automatically¡¡£¨you¡¡will¡¡have¡¡implemented¡¡¡¡
them£©¡£¡¡For¡¡those¡¡conversions¡¡that¡¡cannot¡¡be¡¡done£»¡¡a¡¡cast¡¡exception¡¡is¡¡thrown£»¡¡just¡¡as¡¡the¡¡orig
inal¡¡object¡based¡¡AssignCellState£¨£©¡¡would¡¡have¡¡thrown¡¡an¡¡exception¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡assignment¡¡is¡¡a¡¡series¡¡of¡¡casts¡¡and¡¡conversions¡¡that¡¡you¡¡need¡¡to¡¡do¡¡so¡¡that¡¡you¡¡can¡¡¡¡
convert¡¡one¡¡type¡¡to¡¡another¡£¡¡The¡¡main¡¡problem¡¡when¡¡converting¡¡types¡¡in¡¡this¡¡way¡¡is¡¡that¡¡the¡¡¡¡
method¡level¡¡¡¡generics¡¡parameter¡¡is¡¡a¡¡type¡¡that¡¡is¡¡not¡¡related¡¡to¡¡the¡¡type¡level¡¡¡¡generics¡¡¡¡
parameter¡£¡¡
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£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡step¡¡is¡¡to¡¡convert¡¡the¡¡parameter¡¡into¡¡an¡¡object¡¡so¡¡that¡¡the¡¡cast¡¡operators¡¡that¡¡¡¡
expect¡¡objects¡¡will¡¡work¡¡without¡¡the¡¡Visual¡¡Basic¡¡piler¡¡generating¡¡errors¡£¡¡The¡¡If¡¡statement¡¡¡¡
said¡¡that¡¡the¡¡input¡¡type¡¡is¡¡a¡¡String£»¡¡and¡¡destination¡¡type¡¡is¡¡a¡¡Double¡£¡¡Thus£»¡¡the¡¡methods¡¡CStr£¨£©¡¡¡¡
and¡¡Double¡£Parse£¨£©¡¡are¡¡called¡¡to¡¡convert¡¡the¡¡input¡¡string¡¡to¡¡a¡¡double¡¡number¡£¡¡Once¡¡the¡¡string¡to
double¡¡functions¡¡have¡¡pleted£»¡¡the¡¡CellState¡¡could¡¡be¡¡assigned£»¡¡if¡¡it¡¡were¡¡not¡¡for¡¡the¡¡little¡¡¡¡
problem¡¡of¡¡the¡¡¡¡generics¡type¡¡level¡¡parameter¡£¡¡The¡¡next¡¡steps¡¡of¡¡casting¡¡using¡¡CType£¨£©¡¡and¡¡¡¡
DirectCast£¨£©¡¡are¡¡a¡¡boxing¡¡and¡¡unboxing¡¡of¡¡a¡¡value¡¡type¡£¡¡This¡¡is¡¡necessary¡¡because¡¡the¡¡piler¡¡¡¡
considers¡¡everything¡¡as¡¡an¡¡object£»¡¡and¡¡thus¡¡you¡¡need¡¡to¡¡first¡¡convert¡¡to¡¡an¡¡object¡¡and¡¡then¡¡¡¡
convert¡¡to¡¡the¡¡actual¡¡type¡£¡¡
¡öNote¡¡¡¡The¡¡steps¡¡to¡¡convert¡¡from¡¡one¡¡¡¡generics¡¡type¡¡to¡¡another¡¡¡¡generics¡¡type¡¡would¡¡seem¡¡¡¡
cumbersome£»¡¡and¡¡to¡¡a¡¡degree¡¡they¡¡are¡£¡¡Although¡¡the¡¡steps¡¡illustrated¡¡here¡¡are¡¡the¡¡minimum£»¡¡they¡¡allow¡¡you¡¡¡¡
to¡¡convert¡¡one¡¡type¡¡to¡¡another¡£¡¡The¡¡steps¡¡are¡¡dangerous¡¡in¡¡that¡¡if¡¡you¡¡don¡¯t¡¡know¡¡ahead¡¡of¡¡time¡¡whether¡¡the¡¡¡¡
conversion¡¡is¡¡allowed£»¡¡an¡¡exception¡¡could¡¡result¡£¡¡In¡¡the¡¡example£»¡¡an¡¡exception¡¡is¡¡almost¡¡impossible¡¡because¡¡¡¡
the¡¡types¡¡were¡¡checked¡¡ahead¡¡of¡¡time£»¡¡and¡¡an¡¡appropriate¡¡plan¡¡of¡¡action¡¡was¡¡taken¡£¡¡
¡¡¡¡¡¡¡¡¡¡Overall£»¡¡the¡¡AssignCellState£¨Of¡¡£©£¨£©¡¡method¡¡with¡¡¡¡generics¡¡parameters¡¡provides¡¡the¡¡¡¡
ability¡¡to¡¡cleanly¡¡assign¡¡a¡¡value¡¡to¡¡the¡¡spreadsheet£»¡¡and¡¡a¡¡clean¡¡and¡¡maintainable¡¡method¡¡to¡¡¡¡
perform¡¡a¡¡conversion¡£¡¡This¡¡goes¡¡back¡¡to¡¡the¡¡original¡¡requirement¡¡of¡¡being¡¡able¡¡to¡¡mix¡¡types¡¡safely¡£¡¡
Overriding¡¡the¡¡ToString£¨£©¡¡Functionality¡¡
Debugging¡¡a¡¡data¡¡structure¡¡like¡¡a¡¡spreadsheet¡¡is¡¡a¡¡fairly¡¡plex¡¡task£»¡¡because¡¡there¡¡is¡¡too¡¡¡¡
much¡¡data¡£¡¡The¡¡Worksheet£¨Of¡¡£©¡¡class¡¡implements¡¡the¡¡ToString£¨£©¡¡method¡¡and¡¡generates¡¡a¡¡¡¡
string¡£¡¡The¡¡string¡¡can¡¡be¡¡retrieved¡¡and¡¡then¡¡displayed¡¡using¡¡a¡¡method¡¡like¡¡Console¡£WriteLine£¨£©¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡330¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
308¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Here¡¡is¡¡the¡¡implementation¡¡of¡¡worksheet¡¯s¡¡ToString£¨£©¡¡function¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡ToString£¨£©¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡builder¡¡As¡¡New¡¡StringBuilder£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡row¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨0£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡needma¡¡As¡¡Boolean¡¡=¡¡False¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_generateRowCounter¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡needma¡¡=¡¡True¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨row£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡col¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡col¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨1£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡needma¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨¡¨£»¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡needma¡¡=¡¡True¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡ellState£¨row£»¡¡col£©¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨CellState£¨row£»¡¡col£©¡£ToString£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡col¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡builder¡£Append£¨ChrW£¨10£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡row¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡builder¡£ToString¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Because¡¡the¡¡worksheet¡¡can¡¡be¡¡large£»¡¡StringBuilder¡¡is¡¡used¡¡to¡¡incrementally¡¡build¡¡a¡¡string¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡is¡¡returned¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡ToString£¨£©¡¡method¡¡is¡¡useful¡¡when¡¡you¡¯re¡¡debugging¡¡or¡¡trying¡¡to¡¡perform¡¡analysis¡¡of¡¡the¡¡state¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡an¡¡object¡¡without¡¡actually¡¡debugging¡¡the¡¡program¡£¡¡Thus£»¡¡for¡¡improved¡¡debugging¡¡or¡¡runtime¡¡analysis£»¡¡always¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implement¡¡ToString£¨£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Using¡¡the¡¡Spreadsheet¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡the¡¡interfaces¡¡and¡¡implementations¡¡plete£»¡¡it¡¡is¡¡possible¡¡to¡¡use¡¡the¡¡spreadsheet¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sample¡¡application¡¡is¡¡a¡¡spreadsheet¡¡that¡¡calculates¡¡the¡¡average¡¡of¡¡a¡¡set¡¡of¡¡numbers¡¡and¡¡then¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculates¡¡how¡¡far¡¡each¡¡number¡¡is¡¡from¡¡the¡¡average¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Calculating¡¡an¡¡Average¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡spreadsheet¡¡calculates¡¡the¡¡average¡¡of¡¡a¡¡set¡¡of¡¡numbers£»¡¡and¡¡then¡¡subtracts¡¡the¡¡average¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡from¡¡each¡¡number¡£¡¡The¡¡example¡¡demonstrates¡¡reading¡¡a¡¡plete¡¡spreadsheet¡¡to¡¡get¡¡a¡¡number¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡reading¡¡individual¡¡elements¡¡to¡¡perform¡¡a¡¡calculation¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡331¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡309¡¡
¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡use¡¡the¡¡following¡¡numbers¡¡to¡¡calculate¡¡the¡¡average¡£¡¡
Dim¡¡items¡¡As¡¡Double£¨£©¡¡=¡¡New¡¡Double£¨£©¡¡£û¡¡1¡£0£»¡¡2¡£0£»¡¡3¡£0¡¡£ý¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡average¡¡number¡¡is¡¡2¡£0£»¡¡and¡¡if¡¡you¡¡subtract¡¡the¡¡average¡¡from¡¡each¡¡number£»¡¡you¡¡will¡¡get¡¡¡¡
the¡¡series¡¡¨C1£»¡¡0£»¡¡and¡¡1¡£¡¡
¡¡¡¡¡¡¡¡¡¡To¡¡make¡¡this¡¡work¡¡for¡¡a¡¡spreadsheet£»¡¡the¡¡first¡¡step¡¡is¡¡to¡¡declare¡¡and¡¡then¡¡populate¡¡an¡¡¡¡
IWorksheet£¨Of¡¡BaseType£©¡¡instance¡£¡¡To¡¡instantiate¡¡an¡¡IWorksheet£¨Of¡¡BaseType£©¡¡instance£»¡¡you¡¡¡¡
use¡¡a¡¡factory¡¡that¡¡will¡¡instantiate¡¡the¡¡Worksheet£¨Of¡¡BaseType£©¡¡class¡£¡¡The¡¡code¡¡looks¡¡like¡¡this£º¡¡
Dim¡¡sheetAverage¡¡As¡¡IWorksheet£¨Of¡¡Double£©¡¡=¡¡_¡¡
¡¡¡¡SpreadsheetManager¡£CreateEmptyWorksheet£¨Of¡¡Double£©£¨¡¨¡¨£©¡¡
Dim¡¡items¡¡As¡¡Double£¨£©¡¡=¡¡New¡¡Double£¨£©¡¡£û1£»¡¡2£»¡¡3£ý¡¡
sheetAverage¡£Dimension£¨£¨items¡£Length¡¡£«¡¡10£©£»¡¡3£©¡¡
For¡¡row¡¡=¡¡0¡¡To¡¡items¡£Length¡¡1¡¡
¡¡¡¡¡¡¡¡sheetAverage¡£SetCellState£¨row£»¡¡0£»¡¡items£¨row£©£©¡¡
Next¡¡row¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡worksheet¡¡is¡¡declared¡¡as¡¡being¡¡of¡¡type¡¡Double¡¡£¨IWorksheet£¨Of¡¡Double£©£©£»¡¡allowing¡¡you¡¡¡¡
to¡¡manage¡¡a¡¡Double¡¡value¡£¡¡To¡¡populate¡¡¡¡sheetAverage£»¡¡the¡¡numbers¡¡are¡¡iterated¡¡in¡¡a¡¡For¡¡loop¡¡¡¡
and¡¡assigned¡¡to¡¡the¡¡worksheet¡¡using¡¡the¡¡SetCellState£¨£©¡¡method¡£¡¡The¡¡method¡¡Dimension£¨£©¡¡is¡¡¡¡
needed¡¡to¡¡create¡¡a¡¡fixed¡length¡¡worksheet¡£¡¡
¡¡¡¡¡¡¡¡¡¡With¡¡the¡¡worksheet¡¡populated£»¡¡to¡¡make¡¡sure¡¡everything¡¡looks¡¡right£»¡¡you¡¡could¡¡call¡¡the¡¡method¡¡¡¡
ToString£¨£©¡¡and¡¡see¡¡if¡¡all¡¡is¡¡as¡¡expected¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡next¡¡step¡¡is¡¡to¡¡assign¡¡the¡¡lambda¡¡expressions¡¡that¡¡will¡¡be¡¡used¡¡to¡¡calculate¡¡the¡¡average¡¡¡¡
and¡¡then¡¡the¡¡individual¡¡differences¡¡from¡¡the¡¡average¡£¡¡When¡¡you¡¡assign¡¡a¡¡calculation¡¡to¡¡the¡¡work
sheet£»¡¡you¡¡need¡¡to¡¡know¡¡whether¡¡the¡¡lambda¡¡expression¡¡will¡¡be¡¡stateful¡¡or¡¡stateless¡£¡¡Remember¡¡¡¡
that¡¡lambda¡¡expressions¡¡have¡¡some¡¡state¡£¡¡It¡¡is¡¡just¡¡a¡¡question¡¡of¡¡whether¡¡you¡¡want¡¡a¡¡shared
state¡¡lambda¡¡expression¡¡or¡¡an¡¡individual¡state¡¡lambda¡¡expression¡£¡¡In¡¡the¡¡case¡¡of¡¡the¡¡lambda¡¡¡¡
expressions¡¡for¡¡the¡¡average¡¡calculations£»¡¡a¡¡shared¡¡state¡¡is¡¡acceptable¡£¡¡
¡¡¡¡¡¡¡¡¡¡To¡¡calculate¡¡the¡¡average£»¡¡you¡¡use¡¡a¡¡technique¡¡where¡¡the¡¡average¡¡calculation¡¡is¡¡the¡¡last¡¡element¡¡¡¡
in¡¡the¡¡series¡¡of¡¡the¡¡array¡¡calculations¡£¡¡Thus£»¡¡when¡¡the¡¡average¡¡calculation¡¡is¡¡called£»¡¡it¡¡knows¡¡¡¡
how¡¡many¡¡elements¡¡there¡¡are¡¡because¡¡of¡¡the¡¡row¡¡in¡¡which¡¡the¡¡average¡¡calculation¡¡is¡¡stored¡£¡¡
Function¡¡Average£¨ByVal¡¡worksheet¡¡As¡¡IWorksheet£¨Of¡¡Double£©£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡cellRow¡¡As¡¡Integer£»¡¡ByVal¡¡cellCol¡¡As¡¡Integer£©¡¡
¡¡¡¡¡¡¡¡Dim¡¡runningTotal¡¡As¡¡Double¡¡=¡¡0¡¡
¡¡¡¡¡¡¡¡Dim¡¡row¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡For¡¡row¡¡=¡¡0¡¡To¡¡cellRow¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡runningTotal¡¡=¡¡£¨runningTotal¡¡£«¡¡worksheet¡£GetCellState£¨row£»¡¡0£©£©¡¡
¡¡¡¡¡¡¡¡Next¡¡row¡¡
¡¡¡¡¡¡¡¡Return¡¡£¨runningTotal¡¡/¡¡CDbl£¨cellRow£©£©¡¡
End¡¡Function¡¡
¡£¡¡¡£¡¡¡£¡¡
sheetAverage¡£AssignCellCalculation£¨items¡£Length£»¡¡0£»¡¡AddressOf¡¡Average£©¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡average¡¡is¡¡calculated¡¡by¡¡using¡¡the¡¡variable¡¡cellRow¡¡as¡¡a¡¡maximum¡¡row¡£¡¡¡¡
Every¡¡cell¡¡£¨GetCellState£¨£©£©¡¡before¡¡cellRow¡¡is¡¡added¡¡to¡¡a¡¡running¡¡total¡¡£¨runningTotal£©£»¡¡and¡¡then£»¡¡¡¡
finally£»¡¡an¡¡average¡¡is¡¡returned¡¡by¡¡dividing¡¡runningTotal¡¡by¡¡¡¡cellRow¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡332¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
310¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡the¡¡average¡¡calculated£»¡¡the¡¡next¡¡step¡¡is¡¡to¡¡calculate¡¡the¡¡difference¡¡between¡¡the¡¡average¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡the¡¡individual¡¡items¡£¡¡The¡¡result¡¡will¡¡be¡¡stored¡¡in¡¡a¡¡column¡¡to¡¡the¡¡right¡¡of¡¡the¡¡item¡¡cell¡¡state¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡is¡¡done¡¡by¡¡subtracting¡¡the¡¡average¡¡calculation¡¡cell¡¡state¡¡from¡¡the¡¡item¡¡value£»¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡=¡¡0¡¡To¡¡items¡£Length¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sheetAverage¡£AssignCellCalculation£¨row£»¡¡1£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function£¨worksheet¡¡As¡¡IWorksheet£¨Of¡¡Double£©£»¡¡cellRow¡¡As¡¡Integer£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cellCol¡¡As¡¡Integer£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨worksheet¡£GetCellState£¨cellRow£»¡¡0£©¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡worksheet¡£Calculate£¨items¡£Length£»¡¡0£©£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡row¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡number¡¡of¡¡cell¡state¡¡calculations¡¡depends¡¡on¡¡the¡¡count¡¡of¡¡numbers¡¡in¡¡items¡£¡¡Each¡¡cell¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculation¡¡is¡¡assigned¡¡a¡¡locally¡¡declared¡¡lambda¡¡expression£»¡¡meaning¡¡that¡¡the¡¡lambda¡¡expres
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sions¡¡of¡¡all¡¡cell¡¡states¡¡will¡¡be¡¡identical¡¡and¡¡share¡¡the¡¡same¡¡state¡£¡¡The¡¡only¡¡shared¡¡variable¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡items¡£Length¡£¡¡All¡¡the¡¡lambda¡¡expressions¡¡expect¡¡the¡¡same¡¡length£»¡¡and¡¡so¡¡it¡¡is¡¡acceptable¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡share¡¡this¡¡variable¡£¡¡The¡¡average¡¡difference¡¡is¡¡calculated¡¡by¡¡calculating¡¡the¡¡average¡¡and¡¡then¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡subtracting¡¡it¡¡from¡¡the¡¡worksheet¡¡cell¡¡item¡¡value¡¡that¡¡is¡¡in¡¡the¡¡zeroth¡¡column¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Finally£»¡¡when¡¡everything¡¡is¡¡assigned£»¡¡you¡¡can¡¡call¡¡the¡¡worksheet¡£Calculate£¨£©¡¡method¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculate¡¡the¡¡average¡¡and¡¡difference¡¡from¡¡the¡¡average¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sheetAverage¡£Calculate£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨sheetAverage¡£ToString£¨£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Why¡¡the¡¡Calculation¡¡Worked¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡cell¡¡calculations¡¡work¡¡because¡¡the¡¡spreadsheet¡¡has¡¡the¡¡ability¡¡to¡¡track¡¡what¡¡has¡¡been¡¡calculated¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡what¡¡has¡¡not¡¡been¡¡calculated¡£¡¡In¡¡a¡¡typical¡¡spreadsheet£»¡¡you¡¡can¡¡change¡¡one¡¡cell¡¡in¡¡a¡¡sheet¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡have¡¡everything¡¡magically¡¡recalculate¡£¡¡There¡¡is¡¡no¡¡such¡¡feature¡¡for¡¡this¡¡spreadsheet¡£¡¡However£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this¡¡simpler¡¡spreadsheet¡¡version¡¡can¡¡make¡¡sure¡¡that¡¡when¡¡there¡¡are¡¡dependencies£»¡¡they¡¡are¡¡not¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculated¡¡multiple¡¡times¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Look¡¡back¡¡at¡¡the¡¡source¡¡code¡¡to¡¡calculate¡¡the¡¡difference¡¡between¡¡the¡¡average¡¡and¡¡a¡¡number¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡only¡¡reason¡¡the¡¡calculation¡¡worked¡¡is¡¡that¡¡the¡¡cell¡¡that¡¡contained¡¡the¡¡average¡¡was¡¡called¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡the¡¡Calculate£¨£©¡¡method¡£¡¡Had¡¡the¡¡GetCellState£¨£©¡¡method¡¡been¡¡used£»¡¡the¡¡average¡¡might¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡have¡¡been¡¡calculated£»¡¡and¡¡thus¡¡the¡¡difference¡¡calculation¡¡would¡¡have¡¡been¡¡corrupted¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡But¡¡having¡¡each¡¡and¡¡every¡¡cell¡¡calculate¡¡the¡¡average¡¡whenever¡¡a¡¡small¡¡change¡¡is¡¡made¡¡is¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡waste¡¡of¡¡resources£»¡¡since¡¡the¡¡change¡¡might¡¡not¡¡affect¡¡a¡¡cell¡¡that¡¡is¡¡recalculated¡£¡¡To¡¡avoid¡¡this£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡built¡¡into¡¡the¡¡spreadsheet¡¡is¡¡a¡¡version¡control¡¡mechanism¡¡that¡¡calculates¡¡a¡¡cell¡¡to¡¡the¡¡latest¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡version¡£¡¡Then£»¡¡if¡¡another¡¡calculation¡¡is¡¡called¡¡with¡¡the¡¡same¡¡version¡¡number£»¡¡the¡¡value¡¡is¡¡retrieved¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡from¡¡the¡¡cell¡¡state¡£¡¡The¡¡following¡¡is¡¡the¡¡code¡¡from¡¡Worksheet£¨Of¡¡BaseType£©¡¡that¡¡manages¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡version¡¡number¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡Calculate£¨ByVal¡¡row¡¡As¡¡Integer£»¡¡ByVal¡¡col¡¡As¡¡Integer£©¡¡As¡¡BaseType¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡IWorksheet£¨Of¡¡BaseType£©¡£Calculate¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨CurrVersion¡¡¡·¡¡CalculationVersion£¨row£»¡¡col£©£©¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellState£¨row£»¡¡col£©¡¡=¡¡Cells£¨row£»¡¡col£©£¨Me£»¡¡row£»¡¡col£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CalculationVersion£¨row£»¡¡col£©¡¡=¡¡CurrVersion¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡CellState£¨row£»¡¡col£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡333¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡311¡¡
Public¡¡Sub¡¡Calculate£¨£©¡¡Implements¡¡IWorksheet£¨Of¡¡BaseType£©¡£Calculate¡¡
¡¡¡¡¡¡¡¡CurrVersion¡¡£«=¡¡1¡¡
¡¡¡¡¡¡¡¡Dim¡¡row¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡For¡¡row¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨0£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡col¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡col¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨1£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡Cells£¨row£»¡¡col£©¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Calculate£¨row£»¡¡col£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡col¡¡
¡¡¡¡¡¡¡¡Next¡¡row¡¡
End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡Calling¡¡the¡¡method¡¡Calculate£¨£©¡¡without¡¡parameters¡¡indicates¡¡a¡¡desire¡¡to¡¡recalculate¡¡the¡¡¡¡
entire¡¡spreadsheet¡£¡¡In¡¡the¡¡implementation¡¡of¡¡Calculate£¨£©£»¡¡the¡¡variable¡¡CurrVersion£»¡¡which¡¡¡¡
represents¡¡the¡¡version¡¡number¡¡of¡¡the¡¡current¡¡calculation£»¡¡is¡¡incremented¡£¡¡Then¡¡each¡¡cell¡¡is¡¡iter
ated£»¡¡and¡¡if¡¡it¡¡exists£»¡¡the¡¡individual¡¡cell¡¡form¡¡of¡¡Calculate£¨£©¡¡£¨Calculate£¨£©¡¡with¡¡the¡¡parameters¡¡row¡¡¡¡
and¡¡col£©¡¡is¡¡called¡£¡¡In¡¡the¡¡individual¡¡cell¡¡form¡¡of¡¡Calculate£¨£©£»¡¡a¡¡check¡¡is¡¡made¡¡to¡¡see¡¡if¡¡the¡¡calcu
lation¡¡version¡¡number¡¡of¡¡the¡¡cell¡¡is¡¡the¡¡latest¡¡of¡¡the¡¡spreadsheet£»¡¡if¡¡not£»¡¡the¡¡cell¡¯s¡¡lambda¡¡expression¡¡¡¡
is¡¡called¡£¡¡After¡¡the¡¡new¡¡CellState¡¡has¡¡been¡¡assigned£»¡¡the¡¡cell¡state¡¡version¡¡number¡¡is¡¡incre
mented£»¡¡and¡¡the¡¡cell¡¡state¡¡is¡¡returned¡£¡¡
¡öNote¡¡¡¡The¡¡calculation¡¡of¡¡the¡¡spreadsheet¡¡is¡¡nothing¡¡earth¡shattering£»¡¡and¡¡you¡¡might¡¡be¡¡tempted¡¡to¡¡argue¡¡¡¡
it¡¡is¡¡irrelevant¡¡to¡¡the¡¡scope¡¡of¡¡the¡¡book¡£¡¡In¡¡fact£»¡¡the¡¡calculation¡¡and¡¡its¡¡side¡¡effects¡¡are¡¡of¡¡major¡¡relevance¡£¡¡With¡¡¡¡
lambda¡¡expressions£»¡¡you¡¡have¡¡a¡¡form¡¡of¡¡asynchronous¡¡processing£»¡¡very¡¡much¡¡like¡¡a¡¡spreadsheet¡£¡¡The¡¡cell¡¡¡¡
calculations¡¡of¡¡the¡¡spreadsheet¡¡do¡¡not¡¡know¡¡when¡¡they¡¡will¡¡be¡¡called£»¡¡and¡¡they¡¡cannot¡¡make¡¡assumptions¡¡¡¡
about¡¡the¡¡state¡£¡¡Thus£»¡¡when¡¡a¡¡lambda¡¡expression¡¡is¡¡created£»¡¡the¡¡state¡¡at¡¡the¡¡time¡¡of¡¡the¡¡lambda¡¡expressio
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡