ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ78²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡object¡£¡¡The¡¡identifier¡¡might¡¡be¡¡a¡¡path¡¡or¡¡file¡¡name¡¡and¡¡is¡¡pletely¡¡dependent¡¡on¡¡the¡¡imple
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mentation¡¡of¡¡IWorkbook¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡321¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡299¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡default¡¡property£»¡¡Item£»¡¡is¡¡the¡¡primary¡¡way¡¡of¡¡getting¡¡and¡¡retrieving¡¡worksheets£»¡¡where¡¡¡¡
each¡¡worksheet¡¡is¡¡referenced¡¡using¡¡a¡¡string¡¡identifier¡£¡¡The¡¡identifier¡¡does¡¡not¡¡need¡¡to¡¡be¡¡a¡¡string¡ª¡¡
it¡¡could¡¡have¡¡been¡¡a¡¡custom¡¡type£»¡¡enumeration£»¡¡or¡¡interface¡¡that¡¡is¡¡implemented¡£¡¡Using¡¡a¡¡string¡¡¡¡
keeps¡¡things¡¡simple£»¡¡but¡¡there¡¡are¡¡maintenance¡¡issues¡£¡¡
¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡all¡¡workbooks¡¡have¡¡a¡¡configuration¡¡worksheet¡£¡¡So¡¡for¡¡most¡¡of¡¡the¡¡code£»¡¡the¡¡string¡¡¡¡
identifier¡¡¡¡¡¨configuration¡¨¡¡is¡¡used¡£¡¡However£»¡¡a¡¡new¡¡programmer¡¡decides¡¡to¡¡use¡¡¡¡¡¨Configuration¡¨¡¡¡¡
£¨with¡¡a¡¡capital¡¡C£©¡£¡¡This¡¡slight¡¡change¡¡will¡¡cause¡¡problems¡¡because¡¡¡¡¡¨configuration¡¨¡¡is¡¡meant¡¡to¡¡¡¡
have¡¡a¡¡lowercase¡¡c¡£¡¡Here¡¯s¡¡the¡¡example£º¡¡
Dim¡¡workbook¡¡As¡¡IWorkbook¡¡
Dim¡¡worksheet1¡¡As¡¡IWorksheetBase¡¡=¡¡workbook£¨¡¨configuration¡¨£©¡¡
Dim¡¡worksheet2¡¡As¡¡IWorksheetBase¡¡=¡¡workbook£¨¡¨Configuration¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡This¡¡approach¡¡uses¡¡a¡¡buffer¡¡that¡¡is¡¡typed¡¡in¡¡by¡¡hand¡¡and¡¡is¡¡considered¡¡hard¡coded¡£¡¡The¡¡¡¡
preferred¡¡alternative¡¡is¡¡to¡¡hard¡code¡¡a¡¡structure¡¡that¡¡is¡¡then¡¡referenced¡¡throughout¡¡the¡¡source¡¡¡¡
code£»¡¡like¡¡this£º¡¡
Public¡¡Module¡¡WorksheetIdentifiers¡¡¡¡¡¡
¡¡¡¡¡¡¡¡Public¡¡Const¡¡Configuration¡¡As¡¡String¡¡=¡¡¡¨configuration¡¨¡¡
End¡¡Module¡¡
Dim¡¡workbook¡¡As¡¡IWorkbook¡¡
Dim¡¡worksheet1¡¡As¡¡IWorksheetBase¡¡=¡¡workbook£¨WorksheetIdentifiers¡£Configuration£©¡¡
Dim¡¡worksheet2¡¡As¡¡IWorksheetBase¡¡=¡¡workbook£¨WorksheetIdentifiers¡£Configuration£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡module¡¡WorksheetIdentifiers¡¡still¡¡contains¡¡a¡¡hard¡coded¡¡string¡¡buffer£»¡¡but¡¡this¡¡buffer¡¡¡¡
has¡¡been¡¡centralized¡¡to¡¡a¡¡single¡¡location¡£¡¡The¡¡workbook¡¡default¡¡property¡¡references¡¡the¡¡identifier¡¡¡¡
within¡¡the¡¡class¡¡WorksheetIdentifiers¡£¡¡Thus£»¡¡if¡¡you¡¡change¡¡the¡¡class¡¡WorksheetIdentifiers£»¡¡you¡¡¡¡
also¡¡change¡¡the¡¡identifiers¡¡used¡¡by¡¡the¡¡default¡¡property¡£¡¡That¡¡way£»¡¡the¡¡chances¡¡of¡¡having¡¡a¡¡typo¡¡¡¡
break¡¡an¡¡application¡¡are¡¡reduced¡£¡¡
¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡get¡¡back¡¡to¡¡the¡¡IWorkbook¡¡interface¡¡and£»¡¡in¡¡particular£»¡¡the¡¡default¡¡property¡£¡¡The¡¡default¡¡¡¡
property¡¡is¡¡of¡¡the¡¡type¡¡IWorksheetBase£»¡¡which¡¡is¡¡a¡¡bit¡¡tedious¡¡because¡¡the¡¡IWorksheetBase¡¡inter
face¡¡is¡¡a¡¡rudimentary¡¡interface¡¡and¡¡most¡¡likely¡¡not¡¡the¡¡interface¡¡that¡¡you¡¡will¡¡use£»¡¡because¡¡we¡¡¡¡
have¡¡¡¡IWorksheet£¨Of¡¡BaseType£©£»¡¡and¡¡therein¡¡lies¡¡the¡¡problem¡£¡¡To¡¡get¡¡an¡¡IWorksheet¡¡instance£»¡¡¡¡
you¡¡would¡¡need¡¡a¡¡cast£»¡¡as¡¡follows£º¡¡
Dim¡¡workbook¡¡As¡¡IWorkbook¡¡
Dim¡¡worksheet¡¡As¡¡IWorksheet£¨Of¡¡String£©¡¡=¡¡_¡¡¡¡
¡¡¡¡¡¡¡¡TryCast£¨workbook£¨WorksheetIdentifiers¡£Configuration£©£»¡¡IWorksheet£¨Of¡¡String£©£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡is¡¡the¡¡cast¡¡you¡¡need¡¡each¡¡and¡¡every¡¡time¡¡you¡¡want¡¡to¡¡reference¡¡an¡¡IWorksheet¡¡¡¡
instance¡£¡¡The¡¡cast¡¡is¡¡not¡¡a¡¡big¡¡deal£»¡¡but¡¡it¡¡is¡¡tedious¡£¡¡I¡¡personally¡¡would¡¡prefer¡¡being¡¡able¡¡to¡¡call¡¡¡¡
a¡¡property£»¡¡method£»¡¡or¡¡default¡¡property¡¡that¡¡returns¡¡the¡¡type¡¡I¡¡want¡£¡¡
¡¡¡¡¡¡¡¡¡¡Therein¡¡lies¡¡the¡¡problem£»¡¡because¡¡the¡¡way¡¡I¡¡want¡¡to¡¡use¡¡a¡¡default¡¡property£»¡¡property£»¡¡or¡¡¡¡
method¡¡is¡¡as¡¡a¡¡mixed¡¡type£»¡¡and¡¡you¡¡can¡¯t¡¡define¡¡a¡¡default¡¡property¡¡with¡¡mixed¡¡types¡£¡¡You¡¡can¡¡¡¡
define¡¡a¡¡default¡¡property¡¡that¡¡is¡¡a¡¡fixed¡¡type¡£¡¡To¡¡understand¡¡this¡¡problem£»¡¡look¡¡at¡¡the¡¡following¡¡¡¡
declaration¡¡of¡¡a¡¡workbook£»¡¡which¡¡does¡¡pile¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡322¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
300¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡System¡£Reflection¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡Devspace¡£Trader¡£mon¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Interface¡¡IWorkbook£¨Of¡¡BaseType£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IDebug¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Default¡¡Property¡¡Item£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡As¡¡IWorksheet£¨Of¡¡BaseType£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡Identifier£¨£©¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡declaration¡¡of¡¡IWorkbook£»¡¡a¡¡¡¡generics¡¡type¡¡is¡¡used£»¡¡but¡¡then¡¡the¡¡problem¡¡is¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡default¡¡property¡¡can¡¡return¡¡only¡¡IWorksheet¡¡instances¡¡of¡¡a¡¡single¡¡type£»¡¡such¡¡as¡¡Double¡¡or¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡String¡£¡¡Remember¡¡that¡¡there¡¡are¡¡multiple¡¡worksheet¡¡types¡¡£¨as¡¡illustrated¡¡in¡¡Figure¡¡11¡2£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡we¡¡want¡¡to¡¡do¡¡is¡¡use¡¡method¡level¡¡¡¡generics¡¡declarations£»¡¡like¡¡this£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Interface¡¡IMixedType¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Default¡¡Property¡¡Item£¨Of¡¡BaseType£©£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡IWorksheet£¨Of¡¡BaseType£©¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡with¡¡the¡¡method¡level¡¡declaration¡¡is¡¡that¡¡it¡¡does¡¡not¡¡pile¡£¡¡There¡¡are¡¡two¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ways¡¡to¡¡declare¡¡a¡¡¡¡generics¡¡parameter¡£¡¡The¡¡first¡¡is¡¡what¡¡you¡¡have¡¡seen¡¡the¡¡most¡¡often£»¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡is¡¡at¡¡the¡¡type¡¡level£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡MyType£¨Of¡¡GenericType£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaring¡¡at¡¡the¡¡type¡¡level¡¡means¡¡that¡¡whenever¡¡you¡¡use¡¡the¡¡type¡¡and¡¡specify¡¡a¡¡type¡¡for¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡parameter£»¡¡MyType¡¡bees¡¡fixed¡¡to¡¡a¡¡certain¡¡type¡£¡¡So¡¡say¡¡you¡¡declared¡¡MyType¡¡as¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡MyType¡¡=¡¡New¡¡MyType£¨Of¡¡Integer£©¡¡£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡any¡¡references¡¡to¡¡GenericType¡¡within¡¡MyType¡¡will¡¡bee¡¡¡¡Integer¡£¡¡This¡¡form¡¡of¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡solves¡¡many¡¡problems£»¡¡as¡¡illustrated¡¡in¡¡Chapter¡¡9¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡But¡¡in¡¡the¡¡case¡¡of¡¡the¡¡IWorkbook£»¡¡we¡¡don¡¯t¡¡want¡¡a¡¡fixed¡¡type¡£¡¡We¡¡want¡¡the¡¡ability¡¡to¡¡have¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡collection¡¡type¡¡contain¡¡mixed¡¡types¡¡of¡¡IWorksheet¡£¡¡The¡¡way¡¡to¡¡achieve¡¡that¡¡is¡¡to¡¡use¡¡¡¡generics¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods£»¡¡like¡¡this£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function¡¡Method£¨Of¡¡GenericType£©£¨£©¡¡As¡¡MyType£¨Of¡¡GenericType£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡the¡¡¡¡generics¡¡parameter¡¡is¡¡associated¡¡with¡¡the¡¡method£»¡¡rather¡¡than¡¡the¡¡type¡£¡¡And¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡means¡¡MyType¡¡can¡¡mix¡¡types¡£¡¡So¡¡we¡¡could¡¡have¡¡different¡¡IWorksheet¡¡types¡¡in¡¡a¡¡single¡¡work
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡book¡£¡¡And¡¡wouldn¡¯t¡¡it¡¡be¡¡great¡¡if¡¡there¡¡were¡¡a¡¡default¡¡property¡¡with¡¡mixed¡¡types£¿¡¡But¡¡you¡¡can¡¯t¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡have¡¡¡¡generics¡¡default¡¡properties¡¡and¡¡properties¡¡that¡¡are¡¡not¡¡declared¡¡at¡¡the¡¡type¡¡level¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thus£»¡¡the¡¡use¡¡of¡¡a¡¡¡¡generics¡¡parameter¡¡with¡¡a¡¡default¡¡property¡¡or¡¡property¡¡will¡¡not¡¡work¡£¡¡In¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡my¡¡opinion£»¡¡that¡¡is¡¡a¡¡real¡¡design¡¡flaw¡¡in¡¡Visual¡¡Basic£»¡¡because¡¡it¡¡means¡¡we¡¡need¡¡to¡¡write¡¡code¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡like¡¡this£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡323¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡301¡¡
Imports¡¡Devspace¡£Trader¡£mon¡¡
Imports¡¡System¡£Reflection¡¡
¡¡_¡¡
Public¡¡Interface¡¡IWorkbook¡¡
¡¡¡¡¡¡¡¡Inherits¡¡IDebug¡¡
¡¡¡¡¡¡¡¡Function¡¡GetSheet£¨Of¡¡BaseType£©£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡As¡¡IWorksheet£¨Of¡¡BaseType£©¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡Identifier£¨£©¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡Default¡¡Property¡¡Item£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡As¡¡IWorksheetBase¡¡
End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡modified¡¡declaration£»¡¡the¡¡method¡¡GetSheet£¨£©¡¡acts¡¡like¡¡the¡¡Get¡¡part¡¡of¡¡the¡¡default¡¡¡¡
property£»¡¡but¡¡notice¡¡where¡¡the¡¡¡¡generics¡¡parameter¡¡BaseType¡¡is¡¡declared¡£¡¡The¡¡declaration¡¡¡¡
is¡¡after¡¡the¡¡method¡¡identifier¡¡and¡¡before¡¡the¡¡first¡¡bracket¡£¡¡In¡¡the¡¡case¡¡of¡¡IWorkbook£»¡¡we¡¡use¡¡the¡¡¡¡
method¡level¡¡¡¡generics¡¡parameter¡¡to¡¡allow¡¡the¡¡caller¡¡to¡¡determine¡¡the¡¡type¡¡of¡¡the¡¡work
sheet¡¡instances¡£¡¡The¡¡implementation¡¡of¡¡IWorkbook¡¡has¡¡to¡¡do¡¡nothing¡¡other¡¡than¡¡perform¡¡the¡¡¡¡
appropriate¡¡cast¡£¡¡Method¡level¡¡¡¡generics¡¡parameters¡¡are¡¡great¡¡when¡¡you¡¡are¡¡dealing¡¡with¡¡¡¡
mixed¡¡types£»¡¡as¡¡in¡¡the¡¡case¡¡of¡¡IWorkbook¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡to¡¡retrieve¡¡a¡¡worksheet¡¡that¡¡previously¡¡needed¡¡a¡¡cast¡¡is¡¡rewritten¡¡as¡¡follows£º¡¡
Dim¡¡workbook¡¡As¡¡IWorkbook¡¡
Dim¡¡worksheet¡¡As¡¡IWorksheet£¨Of¡¡String£©¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡workbook¡£GetSheet£¨Of¡¡String£©£¨WorksheetIdentifiers¡£Configuration£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡cast¡¡has¡¡not¡¡disappeared¡¡pletely¡£¡¡It¡¡is¡¡done¡¡for¡¡us¡¡in¡¡the¡¡implementation¡¡of¡¡the¡¡¡¡
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£¨£»£©¡¡
¡¡¡¡
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡