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

µÚ88²¿·Ö

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

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

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 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

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

Äã¿ÉÄÜϲ»¶µÄ