ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ55²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
Thus£»¡¡some¡¡code¡¡that¡¡iterates¡¡the¡¡array¡¡will¡¡need¡¡to¡¡verify¡¡that¡¡the¡¡array¡¡element¡¡is¡¡not¡¡Nothing¡£¡¡¡¡
The¡¡sample¡¡code¡¡generates¡¡the¡¡structure¡¡illustrated¡¡in¡¡Figure¡¡8¡2¡£¡¡
Figure¡¡8¡2¡£¡¡Array¡¡structure¡¡of¡¡referenced¡¡elements¡¡
¡¡¡¡¡¡¡¡¡¡Figure¡¡8¡2¡¡reveals¡¡a¡¡very¡¡important¡¡aspect¡¡of¡¡reference¡¡types£º¡¡an¡¡array¡¡holds¡¡a¡¡reference¡¡to¡¡¡¡
the¡¡object¡¡and¡¡not¡¡the¡¡value¡¡of¡¡the¡¡object¡£¡¡If¡¡the¡¡array¡¡were¡¡value¡¡types£»¡¡then¡¡the¡¡entire¡¡value¡¡¡¡
would¡¡be¡¡stored¡¡in¡¡the¡¡array¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡array¡¡could¡¡just¡¡as¡¡easily¡¡have¡¡been¡¡some¡¡object¡¡with¡¡a¡¡number¡¡of¡¡variables£»¡¡like¡¡this£º¡¡
Class¡¡MyTypeArray¡¡
¡¡¡¡¡¡¡¡Public¡¡Element1¡¡As¡¡MyType¡¡
¡¡¡¡¡¡¡¡Public¡¡Element2¡¡As¡¡MyType¡¡
¡£¡¡¡£¡¡¡£¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡Since¡¡array¡¡elements¡¡are¡¡a¡¡bunch¡¡of¡¡references¡¡stored¡¡in¡¡a¡¡type£»¡¡you¡¡could¡¡make¡¡use¡¡of¡¡this¡¡¡¡
knowledge¡¡to¡¡create¡¡a¡¡type¡¡that¡¡serves¡¡no¡¡other¡¡purpose¡¡than¡¡referencing¡¡a¡¡list¡¡of¡¡elements£»¡¡¡¡
more¡¡monly¡¡called¡¡a¡¡linked¡¡list¡£¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡a¡¡linked¡¡list£»¡¡the¡¡individual¡¡objects¡¡are¡¡linked¡¡together¡¡and¡¡reference¡¡another¡¡element¡¡¡¡
nearby¡£¡¡A¡¡doubly¡¡linked¡¡list¡¡will¡¡only¡¡ever¡¡hold¡¡references¡¡to¡¡two¡¡other¡¡objects£º¡¡the¡¡next¡¡one¡¡and¡¡¡¡
the¡¡previous¡¡one¡£¡¡£¨Another¡¡type¡¡is¡¡a¡¡singly¡¡linked¡¡list£»¡¡which¡¡holds¡¡a¡¡reference¡¡to¡¡only¡¡one¡¡other¡¡¡¡
object£º¡¡the¡¡next¡¡one¡££©¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡a¡¡doubly¡¡linked¡¡list£»¡¡the¡¡type¡¡will¡¡have¡¡data¡¡members¡¡named¡¡NextItem¡¡and¡¡¡¡PrevItem¡£¡¡¡¡
Each¡¡of¡¡these¡¡data¡¡members¡¡is¡¡used¡¡to¡¡reference¡¡another¡¡element¡¡in¡¡the¡¡list£»¡¡as¡¡illustrated¡¡in¡¡¡¡
Figure¡¡8¡3¡£¡¡To¡¡iterate¡¡a¡¡list£»¡¡you¡¡start¡¡from¡¡either¡¡the¡¡left¡¡or¡¡right¡¡side¡¡and¡¡then¡¡go¡¡to¡¡the¡¡NextItem¡¡¡¡
or¡¡PrevItem¡¡data¡¡members£»¡¡respectively¡£¡¡Here¡¡is¡¡an¡¡example£º¡¡
Dim¡¡curr¡¡As¡¡MyType¡¡=¡¡GetHeadOfList£¨£©¡¡
Do¡¡While¡¡£¨curr¡¡IsNot¡¡Nothing£©¡¡
¡¡¡¡¡¡¡¡'¡¡Do¡¡something¡¡with¡¡curr¡¡
¡¡¡¡¡¡¡¡curr¡¡=¡¡curr¡£NextItem¡¡
Loop¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡228¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
206¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡8¡3¡£¡¡Structure¡¡of¡¡doubly¡¡linked¡¡list¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡can¡¡see£»¡¡you¡¡can¡¡easily¡¡add¡¡elements¡¡to¡¡linked¡¡lists¡£¡¡The¡¡drawback¡¡is¡¡that¡¡it¡¡is¡¡expensive¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡find¡¡a¡¡particular¡¡object£»¡¡as¡¡that¡¡requires¡¡iterating¡¡through¡¡the¡¡list¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡For¡¡the¡¡most¡¡part£»¡¡you¡¡will¡¡be¡¡using¡¡the¡¡standard¡¡¡¡List¡¡class£»¡¡but¡¡there¡¡is¡¡also¡¡a¡¡¡¡LinkedList¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡£¡¡If¡¡you¡¯re¡¡interested£»¡¡you¡¡can¡¡find¡¡information¡¡about¡¡the¡¡¡¡version¡¡of¡¡System¡£Collection¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Generics¡£LinkedList¡¡in¡¡the¡¡MSDN¡¡documentation¡£¡¡We¡¯ll¡¡make¡¡our¡¡own¡¡linked¡¡list¡¡to¡¡demonstrate¡¡Visual¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Basic¡¡programming¡¡techniques£»¡¡but¡¡you¡¡should¡¡use¡¡the¡¡standard¡¡collection¡¡classes¡¡in¡¡almost¡¡every¡¡case¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡our¡¡application¡¯s¡¡kernel£»¡¡we¡¯ll¡¡use¡¡a¡¡doubly¡¡linked¡¡list¡¡to¡¡link¡¡together¡¡a¡¡number¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rooms¡¡into¡¡a¡¡set¡¡of¡¡groupings¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Creating¡¡a¡¡Linked¡¡List¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡initial¡¡structure¡¡of¡¡the¡¡BaseLinkedListItem¡¡class¡¡is¡¡as¡¡follows¡¡£¨defined¡¡in¡¡¡¡LibLightingSystem£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡MustInherit¡¡Class¡¡BaseLinkedListItem¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_next¡¡As¡¡BaseLinkedListItem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_prev¡¡As¡¡BaseLinkedListItem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡NextItem£¨£©¡¡As¡¡BaseLinkedListItem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡PrevItem£¨£©¡¡As¡¡BaseLinkedListItem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_prev¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡BaseLinkedListItem¡¡is¡¡declared¡¡as¡¡MustInherit¡¡to¡¡indicate¡¡that¡¡using¡¡this¡¡class¡¡implies¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡must¡¡derive¡¡a¡¡class¡£¡¡¡¡PrevItem¡¡and¡¡NextItem¡¡are¡¡Visual¡¡Basic¡¡properties¡¡that¡¡can¡¡only¡¡read¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡values¡¡of¡¡the¡¡private¡¡data¡¡members¡¡of¡¡_prev¡¡and¡¡_next¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡229¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡H¡¡AP¡¡TE¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡T¡¡O¡¡R¡¡IE¡¡N¡¡TE¡¡D¡¡¡¡¡¡A¡¡R¡¡CH¡¡I¡¡TE¡¡C¡¡TU¡¡R¡¡E¡¡207¡¡
Inserting¡¡and¡¡Removing¡¡Linked¡¡List¡¡Items¡¡
Inserting¡¡an¡¡object¡¡into¡¡a¡¡linked¡¡list¡¡or¡¡removing¡¡an¡¡object¡¡from¡¡a¡¡linked¡¡list¡¡requires¡¡some¡¡¡¡
careful¡¡coding¡£¡¡You¡¡need¡¡to¡¡make¡¡sure¡¡that¡¡the¡¡actions¡¡will¡¡not¡¡corrupt¡¡the¡¡list¡£¡¡This¡¡is¡¡not¡¡a¡¡task¡¡¡¡
that¡¡you¡¡want¡¡to¡¡delegate¡¡to¡¡the¡¡users¡¡of¡¡the¡¡linked¡¡list£»¡¡as¡¡they¡¡could¡¡unintentionally¡¡corrupt¡¡¡¡
the¡¡list¡£¡¡The¡¡following¡¡is¡¡the¡¡code¡¡to¡¡insert¡¡and¡¡remove¡¡an¡¡object¡¡from¡¡a¡¡linked¡¡list£»¡¡and¡¡is¡¡part¡¡¡¡
of¡¡the¡¡BaseLinkedListItem¡¡class¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Insert£¨ByVal¡¡item¡¡As¡¡BaseLinkedListItem£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡item¡£_next¡¡=¡¡_next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡item¡£_prev¡¡=¡¡Me¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_next¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_next¡£_prev¡¡=¡¡item¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_next¡¡=¡¡item¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Remove£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_next¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_next¡£_prev¡¡=¡¡_prev¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡_prev¡¡IsNot¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_prev¡£_next¡¡=¡¡_next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_next¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_prev¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡Insert£¨£©¡¡method¡¡assumes¡¡that¡¡you¡¡want¡¡to¡¡insert¡¡an¡¡object¡¡into¡¡the¡¡head¡¡of¡¡a¡¡list£»¡¡which¡¡¡¡
has¡¡at¡¡least¡¡one¡¡element¡£¡¡The¡¡Insert£¨£©¡¡method¡¡assumes¡¡the¡¡following¡¡code¡¡at¡¡a¡¡minimum¡£¡¡
Dim¡¡singleElement¡¡As¡¡BaseLinkedListItem¡¡=¡¡GetHeadOfList£¨£©¡¡
Dim¡¡anotherElement¡¡as¡¡BaseLinkedListItem¡¡=¡¡CreateListElement£¨£©¡¡
singleElement¡£Insert£¨anotherElement£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡step¡¡is¡¡to¡¡assign¡¡the¡¡data¡¡members¡¡£¨_next£»¡¡_prev£©¡¡of¡¡the¡¡object¡¡£¨¡¡item£©¡¡that¡¡is¡¡going¡¡¡¡
to¡¡be¡¡added¡¡to¡¡the¡¡list¡£¡¡
¡öNote¡¡¡¡Notice¡¡how¡¡in¡¡the¡¡Insert£¨£©¡¡method£»¡¡it¡¡is¡¡possible¡¡to¡¡assign¡¡the¡¡private¡¡data¡¡members¡¡of¡¡another¡¡¡¡
object¡¡instance¡£¡¡You¡¡learned¡¡that¡¡private¡¡scope¡¡means¡¡that¡¡only¡¡the¡¡declared¡¡type¡¡can¡¡read¡¡private¡¡properties¡¡¡¡
and¡¡methods¡£¡¡This¡¡rule¡¡has¡¡not¡¡been¡¡violated£»¡¡because¡¡the¡¡rule¡¡implies¡¡types¡¡can¡¡read¡¡the¡¡private¡¡data¡¡members¡¡¡¡
and¡¡private¡¡methods¡¡of¡¡other¡¡instances¡¡of¡¡that¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡230¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
208¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡the¡¡data¡¡members¡¡of¡¡the¡¡item¡¡have¡¡been¡¡assigned£»¡¡the¡¡item¡¡is¡¡integrated¡¡into¡¡the¡¡list£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡by¡¡redirecting¡¡the¡¡_prev¡¡property¡¡of¡¡the¡¡next¡¡object¡¡£¨if¡¡it¡¡is¡¡not¡¡Nothing£©£»¡¡and¡¡then¡¡assigning¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡local¡¡_next¡¡property¡¡to¡¡the¡¡object¡¡to¡¡be¡¡inserted¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Remove£¨£©¡¡method¡¡does¡¡the¡¡same¡¡as¡¡¡¡Insert£¨£©£»¡¡but¡¡in¡¡reverse¡£¡¡The¡¡first¡¡step¡¡is¡¡to¡¡redirect¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡_next¡¡and¡¡_prev¡¡properties¡¡of¡¡the¡¡previous¡¡and¡¡next¡¡objects£»¡¡assuming¡¡that¡¡they¡¡are¡¡not¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Nothing¡£¡¡And¡¡then¡¡the¡¡object¡¡to¡¡be¡¡removed¡¡has¡¡its¡¡_next¡¡and¡¡_prev¡¡properties¡¡assigned¡¡to¡¡Nothing¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡declaration¡¡of¡¡PrevItem¡¡and¡¡NextItem¡¡is¡¡a¡¡mon¡¡architecture¡¡where¡¡data¡¡members¡¡can¡¡be¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡read£»¡¡but¡¡to¡¡assign¡¡them£»¡¡you¡¡need¡¡to¡¡use¡¡methods¡£¡¡Using¡¡read¡only¡¡properties¡¡is¡¡one¡¡way¡¡to¡¡make¡¡sure¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡internal¡¡state¡¡cannot¡¡be¡¡corrupted£»¡¡if¡¡you¡¡must¡¡expose¡¡the¡¡internal¡¡state¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Testing¡¡the¡¡Linked¡¡List¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡BaseLinkedListItem¡¡is¡¡a¡¡core¡¡class¡¡and¡¡serves¡¡a¡¡utility¡¡purpose¡£¡¡This¡¡makes¡¡the¡¡class¡¡eligible¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡declared¡¡in¡¡the¡¡kernel¡¡or¡¡a¡¡definitions¡¡assembly¡£¡¡As¡¡the¡¡class¡¡is¡¡a¡¡core¡¡class£»¡¡it¡¡means¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡need¡¡a¡¡more¡¡exhaustive¡¡testing¡¡framework¡¡to¡¡ensure¡¡that¡¡there¡¡will¡¡be¡¡no¡¡problems¡£¡¡In¡¡this¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡section£»¡¡we¡¡will¡¡go¡¡through¡¡one¡¡test¡¡that¡¡demonstrates¡¡what¡¡you¡¡should¡¡be¡¡testing¡¡for¡¡and¡¡how¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡should¡¡be¡¡testing¡¡a¡¡core¡¡class¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡BaseLinkedListItem¡¡is¡¡declared¡¡as¡¡MustInherit¡¡and¡¡needs¡¡an¡¡implementation¡£¡¡The¡¡purpose¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡implementation¡¡is¡¡to¡¡give¡¡us¡¡enough¡¡information¡¡on¡¡the¡¡state¡¡and¡¡context¡¡of¡¡the¡¡object¡£¡¡In¡¡this¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡case£»¡¡we¡¡want¡¡to¡¡define¡¡an¡¡object¡¡that¡¡tests¡¡every¡¡part¡¡of¡¡the¡¡class¡¡BaseLinkedListItem¡£¡¡If¡¡you¡¡have¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ever¡¡seen¡¡pictures¡¡of¡¡a¡¡test¡¡car¡¡tire¡¡hooked¡¡up¡¡to¡¡dozens¡¡of¡¡wires£»¡¡then¡¡you¡¡will¡¡understand¡¡what¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡test¡¡class¡¡needs¡¡to¡¡do¡£¡¡Following¡¡is¡¡a¡¡sample¡¡implementation£»¡¡placed¡¡in¡¡the¡¡TestLightingSystem¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡project¡£¡¡Remember¡¡to¡¡include¡¡a¡¡reference¡¡to¡¡¡¡LibLightingSystem¡¡£¨right¡click¡¡References¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestLightingSystem¡¡and¡¡select¡¡Add¡¡Reference¡¡and¡¡then¡¡Projects¡¡¡¡LibLightingSystem£©¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡LibLightingSystem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡LinkedItem¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡BaseLinkedListItem¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_identifier¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡identifier¡¡As¡¡String£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_identifier¡¡=¡¡identifier¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Identifier¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_identifier¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡231¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡H¡¡AP¡¡TE¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡T¡¡O¡¡R¡¡IE¡¡N¡¡TE¡¡D¡¡¡¡¡¡A¡¡R¡¡CH¡¡I¡¡TE¡¡C¡¡TU¡¡R¡¡E¡¡209¡¡
¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡ToString£¨£©¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡buffer¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡¡¨Me£¨¡¨¡¡&¡¡_identifier¡¡&¡¡¡¨£©¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡NextItem¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡buffer¡¡&¡¡¡¨¡¡next£¨¡¨¡¡&¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡DirectCast£¨MyBase¡£NextItem£»¡¡LinkedItem£©¡£Identifier¡¡&¡¡¡¨£©¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡buffer¡¡&¡¡¡¨¡¡next£¨Nothing£©¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡PrevItem¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡buffer¡¡&¡¡¡¨¡¡prev£¨¡¨¡¡&¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡DirectCast£¨MyBase¡£PrevItem£»¡¡LinkedItem£©¡£Identifier¡¡&¡¡¡¨£©¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡buffer¡¡=¡¡buffer¡¡&¡¡¡¨¡¡prev£¨Nothing£©¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡buffer¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡¡¡LinkedItem¡¡class¡¡has¡¡a¡¡single¡¡data¡¡member¡¡declaration£»¡¡_identifier£»¡¡which¡¡is¡¡used¡¡¡¡
to¡¡identify¡¡the¡¡instance¡£¡¡The¡¡test¡¡code¡¡will¡¡call¡¡the¡¡Insert£¨£©¡¡and¡¡Remove£¨£©¡¡methods£»¡¡and¡¡then¡¡¡¡
generate¡¡a¡¡visual¡¡representation¡¡of¡¡the¡¡linked¡¡list¡£¡¡The¡¡visual¡¡representation¡¡is¡¡used¡¡to¡¡under
stand¡¡what¡¡the¡¡problem¡¡might¡¡be¡¡if¡¡anything¡¡goes¡¡wrong¡£¡¡You¡¡will¡¡not¡¡write¡¡tests¡¡against¡¡the¡¡¡¡
visual¡¡representation¡¡because¡¡that¡¡would¡¡overplicate¡¡the¡¡testing¡£¡¡
¡¡¡¡¡¡¡¡¡¡To¡¡generate¡¡a¡¡visual¡¡representation¡¡of¡¡the¡¡object£»¡¡the¡¡ToString£¨£©¡¡method¡¡is¡¡overridden¡£¡¡By¡¡¡¡
default£»¡¡all¡¡objects¡¡have¡¡a¡¡ToString£¨£©¡¡implementation£»¡¡which¡¡does¡¡absolutely¡¡nothing¡¡other¡¡¡¡
than¡¡spit¡¡out¡¡the¡¡identifier¡¡of¡¡the¡¡object¡¡reference¡£¡¡To¡¡make¡¡ToString£¨£©¡¡do¡¡something¡¡useful£»¡¡¡¡
you¡¡need¡¡to¡¡override¡¡it¡£¡¡In¡¡the¡¡example£»¡¡ToString£¨£©¡¡will¡¡generate¡¡a¡¡buffer¡¡that¡¡contains¡¡the¡¡¡¡
LinkedItem¡¡identifier¡¡and¡¡the¡¡next¡¡and¡¡previous¡¡object¡¡identifiers¡£¡¡These¡¡three¡¡pieces¡¡of¡¡infor
mation¡¡tell¡¡you¡¡the¡¡structure¡¡of¡¡the¡¡linked¡¡list¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡next¡¡step¡¡is¡¡to¡¡write¡¡a¡¡test¡¡in¡¡TestLightingSystem¡¯s¡¡Module1¡£vb¡¡that¡¡verifies¡¡that¡¡the¡¡¡¡
Insert£¨£©¡¡method¡¡works¡¡properly£»¡¡and¡¡it¡¡is¡¡implement
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡