资源描述:
《VB2005微软官方教程03.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C0262058x.fmPage33Wednesday,July28,20043:40PMChapter2LanguageEnhancementsforVisualBasic2005Inthischapter:Application:Generics..................................................33Application:TheMyObject.............................................41Application:Operators.........
2、........................................50Application:Keywords.................................................56VisualBasic2005introducesanumberofenhancementsdesignedtomakeyourcodingsim-plerandmoreefficient.Inthischapter,you’reintroducedtofourofthoseenhancements.Genericsprovi
3、deameansforyoutodesignclassesthatcanhandleobjectsofdifferentkindsefficientlyandinatype-safeway.TheMyObjectoffersconvenientshortcutsforaccessingBaseClassLibraryclassesthatrefertothecurrentlyrunningapplication,thelocalmachine,theapplication’scollectionofforms,thelogged-onuser,an
4、dWebservicesassociatedwiththeapplication.Byoverloadingoperatorssuchas=(equalto)and>(greaterthan),youcannowdefinewhathappenswhentwoinstancesofyourclassarecompared.Andfinally,you’llreviewsomenewkeywords.Application:GenericsThisapplicationillustratesthenewMicrosoftVisualBasic2005
5、supportforgenerics.NewConceptsBeforegettingintotheimplementationofgenerics,it’sworthspendingaminutetoanalyzewhythisfeaturehasbeenaddedtoVisualBasic2005.Genericscomefromtheneedtodealwithpotentiallyanytypeofobjectina“generic”way.Forexample,youmightwanttocreateaclassthatcanaccept
6、onlyEmployeeobjectsorCustomerobjects,andcanprocesseachonedif-ferentlybydistinguishingbetweenthem.33C0262058x.fmPage34Wednesday,July28,20043:40PM34IntroducingMicrosoftVisualBasic2005forDevelopersEvenwithoutgenerics,VisualBasichasalwaysprovidedsomemechanismsfordealingwithobjects
7、,regardlessoftheirtype.Forexample,withVisualBasic6.0,youcanstoreobjectsofanytypeinaCollectionobject.Thistechniquehassomedisadvantages,whicharedescribedinthenextsection.GenericsprovideanefficientalternativetoCollections,enforcingtypesafety,providingbetterperformance,andintegrat
8、ingtightlywiththeMicrosoftIntelliSensetechnology.VisualBasic6