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

µÚ117²¿·Ö

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

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

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡




¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Customer£¨£©¡¡With¡¡£û¡£Identifier¡¡=¡¡¡¨Person¡¡2¡¨£»¡¡¡£Points¡¡=¡¡10£ý£ý¡¡



¡¡¡¡¡¡¡¡¡¡To¡¡get¡¡a¡¡list¡¡of¡¡all¡¡unique¡¡customers£»¡¡you¡¡can¡¡use¡¡Union£¨£©£»¡¡as¡¡follows£º¡¡



Dim¡¡uniqueCustomers¡¡=¡¡customers1¡£Union£¨customers2£©¡¡



¡¡¡¡¡¡¡¡¡¡Contained¡¡within¡¡the¡¡list¡¡represented¡¡by¡¡the¡¡variable¡¡uniqueCustomers¡¡will¡¡be¡¡the¡¡three¡¡¡¡

customers¡¡of¡¡the¡¡two¡¡lists¡£¡¡



Using¡¡LINQ¡¡in¡¡Other¡¡Contexts¡¡



So¡¡far£»¡¡all¡¡of¡¡the¡¡examples¡¡in¡¡this¡¡chapter¡¡involved¡¡using¡¡LINQ¡¡and¡¡objects¡£¡¡However£»¡¡LINQ¡¡is¡¡¡¡

not¡¡just¡¡an¡¡object¡­searching¡¡technology¡£¡¡It¡¡is¡¡also¡¡usable¡¡with¡¡XML¡¡and¡¡relational¡¡databases¡£¡¡¡¡

Using¡¡LINQ¡¡with¡¡these¡¡other¡¡data¡¡sources¡¡is¡¡not¡¡a¡¡problem£»¡¡since¡¡the¡¡querying¡¡is¡¡identical¡£¡¡¡¡

What¡¡is¡¡a¡¡problem¡¡is¡¡getting¡¡the¡¡query¡¡to¡¡work¡¡in¡¡the¡¡first¡¡place¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Consider¡¡Figure¡¡15¡­1£»¡¡which¡¡illustrates¡¡the¡¡LINQ¡¡architecture¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡can¡¡see¡¡in¡¡Figure¡¡15¡­1£»¡¡all¡¡¡¡programming¡¡languages¡¡can¡¡access¡¡the¡¡LINQ¡¡¡¡

library¡£¡¡The¡¡data¡¡manipulated¡¡by¡¡the¡¡LINQ¡¡library¡¡es¡¡from¡¡what¡¡is¡¡called¡¡a¡¡LINQ¡­enabled¡¡¡¡

data¡¡source¡£¡¡The¡¡examples¡¡that¡¡you¡¯ve¡¡seen¡¡use¡¡the¡¡LINQ¡¡to¡¡objects¡¡data¡¡source¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡436¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

414¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡15¡­1¡£¡¡LINQ¡¡architecture¡¡£¨based¡¡on¡¡an¡¡image¡¡in¡¡MSDN¡¡Magazine£»¡¡http£º//msdn¡£microsoft¡£/¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡msdnmag/issues/07/06/csharp30/default¡£aspx£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡there¡¡is¡¡also¡¡the¡¡possibility¡¡to¡¡use¡¡a¡¡LINQ¡­enabled¡¡ADO¡¡connection¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡good¡¡news¡¡is¡¡that¡¡you¡¡can¡¡use¡¡LINQ¡¡with¡¡a¡¡relational¡¡database¡£¡¡The¡¡bad¡¡news¡¡is¡¡that¡¡the¡¡rela

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tional¡¡database¡¯s¡¡ADO¡¡driver¡¡must¡¡support¡¡the¡¡special¡¡LINQ¡¡characteristics¡£¡¡At¡¡the¡¡time¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡this¡¡writing£»¡¡only¡¡the¡¡Microsoft¡¡SQL¡¡Server¡¡driver¡¡supports¡¡LINQ¡£¡¡Currently£»¡¡the¡¡drivers¡¡for¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Microsoft¡¡Access£»¡¡MySQL£»¡¡and¡¡other¡¡relational¡¡databases¡¡do¡¡not¡¡support¡¡LINQ¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Consider¡¡this¡¡LINQ¡¡query£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡northwind¡¡As¡¡NorthwindDataContext¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡NorthwindDataContext£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡products¡¡=¡¡From¡¡p¡¡In¡¡northwind¡£Products¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Where¡¡p¡£OrderDetails¡£Count¡¡=¡¡0¡¡And¡¡p¡£UnitPrice¡¡¡·¡¡100¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select¡¡p¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Notice¡¡the¡¡code¡¡in¡¡the¡¡From¡¡statement¡£¡¡The¡¡data¡¡source¡¡is¡¡an¡¡object¡¡that¡¡references¡¡the¡¡rela

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tional¡¡database¡¡Products¡¡table¡£¡¡If¡¡a¡¡database¡¡driver¡¡is¡¡optimized¡¡for¡¡LINQ£»¡¡it¡¡will¡¡understand¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡LINQ¡¡query¡¡and¡¡optimize¡¡it¡¡as¡¡if¡¡it¡¡were¡¡a¡¡SQL¡¡statement¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡your¡¡database¡¡driver¡¡does¡¡not¡¡support¡¡LINQ£»¡¡then¡¡you¡¡have¡¡a¡¡problem¡¡because£»¡¡in¡¡theory£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡would¡¡need¡¡to¡¡download¡¡all¡¡the¡¡data¡¡from¡¡the¡¡table£»¡¡and¡¡then¡¡execute¡¡the¡¡LINQ¡¡query¡£¡¡That¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡would¡¡waste¡¡resources¡¡and¡¡is¡¡not¡¡remended¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡437¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡415¡¡



¡öNote¡¡¡¡For¡¡examples¡¡of¡¡LINQ¡¡using¡¡relational¡¡databases£»¡¡see¡¡Beginning¡¡VB¡¡2008¡¡Databases¡¡by¡¡Vidya¡¡Vrat¡¡¡¡

Agarwal¡¡and¡¡James¡¡Huddleston¡¡£¨Apress£»¡¡2008£©¡£¡¡



¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡that¡¡you¡¡want¡¡to¡¡execute¡¡LINQ¡¡on¡¡an¡¡XML¡¡document¡£¡¡Consider¡¡the¡¡following¡¡¡¡

XML¡¡LINQ¡¡code¡¡£¨from¡¡¡¡http£º//hookedonlinq¡£/LINQtoXML5MinuteOverview¡£ashx£©¡£¡¡



Dim¡¡loaded¡¡As¡¡XDocument¡¡=¡¡XDocument¡£Load£¨¡¨C£ºcontacts¡£xml¡¨£©¡¡



¡¡'¡¡Query¡¡the¡¡data¡¡and¡¡write¡¡out¡¡a¡¡subset¡¡of¡¡contacts¡¡

Dim¡¡q¡¡=¡¡From¡¡c¡¡In¡¡loaded¡£Descendants£¨¡¨contact¡¨£©¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Where¡¡CType£¨c¡£Attribute£¨¡¨contactId¡¨£©¡£Value£»¡¡Integer£©¡¡¡¶¡¡4¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select¡¡c¡£Element£¨¡¨firstName¡¨£©¡£ToString£¨£©¡¡&¡¡¡¨¡¡¡¨¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡c¡£Element£¨¡¨lastName¡¨£©¡£ToString£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡Notice¡¡how¡¡the¡¡same¡¡LINQ¡¡syntax¡¡that¡¡you¡¯ve¡¡seen¡¡in¡¡the¡¡previous¡¡examples¡¡is¡¡used£»¡¡but¡¡¡¡

the¡¡source¡¡of¡¡the¡¡data¡¡that¡¡is¡¡to¡¡be¡¡manipulated¡¡by¡¡LINQ¡¡is¡¡different¡£¡¡Keep¡¡in¡¡mind¡¡that¡¡when¡¡¡¡

you¡¡are¡¡manipulating¡¡data¡¡using¡¡LINQ£»¡¡you¡¡are¡¡manipulating¡¡objects¡¡that¡¡may¡¡point¡¡to¡¡XML¡¡¡¡

files£»¡¡relational¡¡databases£»¡¡or¡¡plain¡­vanilla¡¡data¡¡objects¡£¡¡



The¡¡Important¡¡Stuff¡¡to¡¡Remember¡¡



In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡about¡¡the¡¡basics¡¡of¡¡LINQ¡¡and¡¡how¡¡to¡¡write¡¡queries¡£¡¡Here¡¡are¡¡the¡¡key¡¡¡¡

points¡¡to¡¡remember£º¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡LINQ¡¡is¡¡an¡¡API¡¡that¡¡sits¡¡on¡¡top¡¡of¡¡other¡¡technologies¡¡such¡¡as¡¡Visual¡¡Basic¡¡objects£»¡¡rela

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tional¡¡databases£»¡¡and¡¡XML¡¡documents¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡LINQ¡¡can¡¡work¡¡effectively¡¡only¡¡if¡¡the¡¡underlying¡¡data¡¡source¡¡technology¡¡has¡¡been¡¡opti

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mized¡¡for¡¡LINQ¡£¡¡Otherwise£»¡¡you¡¡are¡¡left¡¡with¡¡having¡¡to¡¡load¡¡a¡¡single¡¡record¡¡set¡¡and¡¡then¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡manipulate¡¡that¡¡record¡¡set¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Regardless¡¡of¡¡the¡¡data¡¡source£»¡¡the¡¡techniques¡¡used¡¡to¡¡query¡¡and¡¡write¡¡LINQ¡¡are¡¡identical¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡manipulating¡¡LINQ¡¡objects£»¡¡the¡¡methods¡¡and¡¡properties¡¡associated¡¡with¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡various¡¡data¡¡sources¡¡are¡¡different¡£¡¡For¡¡example£»¡¡when¡¡searching¡¡XML¡¡documents£»¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡use¡¡XML¡¡Document¡¡Object¡¡Model¡¡£¨DOM£©¡¡methods¡¡and¡¡properties¡¡that¡¡are¡¡not¡¡avail

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡able¡¡when¡¡manipulating¡¡plain¡­vanilla¡¡objects¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡LINQ¡¡is¡¡not¡¡just¡¡a¡¡syntax£»¡¡but¡¡a¡¡series¡¡of¡¡extension¡¡methods¡¡associated¡¡with¡¡sets¡¡of¡¡data¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡methods¡¡allow¡¡for¡¡more¡¡sophisticated¡¡data¡¡pipelining¡¡and¡¡processing¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡information¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡438¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

416¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡5¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡N¡¡Q¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Some¡¡Things¡¡for¡¡You¡¡to¡¡Do¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡are¡¡two¡¡exercises¡¡to¡¡help¡¡you¡¡apply¡¡what¡¡you¡¯ve¡¡learned¡¡in¡¡this¡¡chapter¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡The¡¡solution¡¡for¡¡finding¡¡a¡¡frequency¡¡presented¡¡in¡¡this¡¡chapter¡¡went¡¡from¡¡text¡¡to¡¡text¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculate¡¡the¡¡statistics¡£¡¡Can¡¡you¡¡think¡¡of¡¡another¡¡approach¡¡that¡¡would¡¡require¡¡minimal¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡changes¡¡in¡¡the¡¡interface¡¡structure£¿¡¡Hint£º¡¡the¡¡way¡¡the¡¡objects¡¡were¡¡parsed¡¡into¡¡objects¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡borrowed¡¡code¡¡from¡¡another¡¡application¡£¡¡Could¡¡that¡¡other¡¡application¡¡be¡¡used¡¡somehow£¿¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡You¡¡saw¡¡a¡¡LINQ¡¡query¡¡embedding¡¡another¡¡LINQ¡¡query¡¡when¡¡finding¡¡the¡¡frequency¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡two¡¡numbers¡£¡¡Rewrite¡¡the¡¡code¡¡to¡¡generate¡¡the¡¡frequency¡¡of¡¡all¡¡binations¡¡of¡¡single£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pairs£»¡¡and¡¡triples¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡439¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

C¡¡¡¡H¡¡¡¡A¡¡¡¡P¡¡¡¡T¡¡¡¡E¡¡¡¡R¡¡¡¡¡¡¡¡¡¡1¡¡¡¡6¡¡



¡ö¡¡¡ö¡¡¡ö¡¡



Learning¡¡About¡¡Other¡¡Visual¡¡¡¡

Basic¡¡Techniques¡¡



This¡¡last¡¡chapter¡¡in¡¡the¡¡book¡¡is¡¡about¡¡tying¡¡up¡¡loose¡¡ends¡£¡¡The¡¡techniques¡¡discussed¡¡in¡¡this¡¡¡¡

chapter¡¡are¡¡those¡¡that¡¡you¡¡will¡¡use¡¡in¡¡specific¡¡situations¡£¡¡This¡¡chapter¡¡covers¡¡the¡¡following¡¡¡¡

topics£º¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡How¡¡to¡¡use¡¡arithmetic¡¡operators¡¡to¡¡manipulate¡¡numbers¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡How¡¡to¡¡overload¡¡operators¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡might¡¡use¡¡the¡¡GoTo¡¡statement¡¡¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡How¡¡to¡¡use¡¡¡¡generics¡¡constraints¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡How¡¡to¡¡use¡¡nullable¡¡types¡¡¡¡



¡¡¡¡¡¡¡¡o¡¡¡¡How¡¡to¡¡use¡¡partial¡¡classes¡¡and¡¡methods¡¡¡¡



Operators¡¡



You¡¡have¡¡seen¡¡various¡¡operators¡¡used¡¡in¡¡examples¡¡throughout¡¡the¡¡book£»¡¡such¡¡as¡¡the¡¡assignment¡¡¡¡

operator¡¡£¨a¡¡=¡¡3£©£»¡¡and¡¡the¡¡logical¡¡operators¡¡£¨¡¡If£¨¡¡a¡¡=¡¡b£©£©¡£¡¡Visual¡¡Basic¡¡has¡¡many¡¡more¡¡arithmetic¡¡¡¡

operators¡¡that¡¡you¡¡can¡¡use¡¡to¡¡process¡¡types¡£¡¡You¡¡can¡¡also¡¡define¡¡custom¡¡operators¡£¡¡¡¡



Using¡¡Arithmetic¡¡Operators¡¡



The¡¡subtraction¡¡£¨¡¡¡­£©£»¡¡multiplication¡¡£¨*£©£»¡¡and¡¡division¡¡£¨/£©¡¡operators¡¡are¡¡typically¡¡applicable¡¡to¡¡¡¡

only¡¡numeric¡¡values¡£¡¡These¡¡operators¡¡are¡¡directly¡¡parable¡¡to¡¡the¡¡mathematical¡¡operators¡¡¡¡

you¡¡learned¡¡about¡¡in¡¡elementary¡¡school¡£¡¡Let¡¯s¡¡look¡¡at¡¡what¡¡the¡¡other¡¡arithmetic¡¡operators¡¡do¡£¡¡



Addition¡¡



The¡¡addition¡¡£¨£«£©¡¡operator¡¡is¡¡used¡¡to¡¡indicate¡¡the¡¡addition¡¡of¡¡two¡¡values£»¡¡like¡¡this£º¡¡



a¡¡=¡¡c¡¡£«¡¡1¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡addition¡¡has¡¡a¡¡left¡­hand¡¡side¡¡and¡¡right¡­hand¡¡side£»¡¡separated¡¡by¡¡the¡¡equal¡¡sign¡¡£¨=£©¡£¡¡¡¡

On¡¡the¡¡right¡­hand¡¡side£»¡¡the¡¡variable¡¡c¡¡is¡¡added¡¡to¡¡¡¡1¡¡and¡¡assigned¡¡to¡¡the¡¡variable¡¡a¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡417¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡440¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

418¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡6¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡O¡¡TH¡¡E¡¡R¡¡¡¡¡¡V¡¡IS¡¡U¡¡AL¡¡¡¡¡¡B¡¡A¡¡SI¡¡C¡¡¡¡TE¡¡C¡¡H¡¡N¡¡IQ¡¡U¡¡E¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡notion¡¡of¡¡a¡¡left¡­hand¡¡side¡¡and¡¡a¡¡right¡­hand¡¡side¡¡as¡¡two¡¡separate¡¡parts¡¡is¡¡important¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡when¡¡you¡¡consider¡¡this¡¡code£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡=¡¡a¡¡£«¡¡1¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡variable¡¡a¡¡is¡¡added¡¡with¡¡the¡¡value¡¡1¡¡and¡¡assigned¡¡to¡¡the¡¡variable¡¡a£»¡¡but¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡these¡¡operations¡¡do¡¡not¡¡happen¡¡at¡¡the¡¡same¡¡time£»¡¡they¡¡happen¡¡sequentially¡£¡¡First£»¡¡the¡¡right

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡hand¡¡side¡¡is¡¡executed£»¡¡and¡¡then¡¡the¡¡left¡­hand¡¡side¡¡is¡¡executed¡£¡¡By¡¡executing¡¡the¡¡left¡­hand¡¡side£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡existing¡¡value¡¡of¡¡the¡¡variable¡¡a¡¡is¡¡overwritten¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡consider¡¡another¡¡example£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡b¡¡=¡¡a¡¡=¡¡a¡¡£«¡¡1¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡were¡¡to¡¡run¡¡this¡¡code£»¡¡b¡¡would¡¡not¡¡be¡¡assigned£»¡¡because¡¡the¡¡example¡¡has¡¡mixed¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡operators£»¡¡which¡¡translates¡¡to¡¡this£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡b¡¡As¡¡Integer¡¡=¡¡CInt£¨¡­£¨£¨a¡¡=¡¡£¨a¡¡£«¡¡1£©£©¡¡¡·¡¡False£©£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡result¡¡is¡¡not¡¡what¡¡you¡¡expected£»¡¡and¡¡not¡¡even¡¡close¡¡to¡¡what¡¡you¡¡were¡¡trying¡¡to¡¡achieve¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨and¡¡isn¡¯t¡¡even¡¡allowed¡¡with¡¡Option¡¡Strict¡¡set¡¡to¡¡On£©¡£¡¡What¡¡this¡¡illustrates¡¡is¡¡that¡¡you¡¡need¡¡to¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡careful¡¡with¡¡operators¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Bitwise¡¡Operators¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Bitwise¡¡operators¡¡are¡¡used¡¡to¡¡access¡¡and¡¡manipulate¡¡individual¡¡bits¡¡in¡¡a¡¡whole¡¡number¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡learned¡¡in¡¡Chapter¡¡2£»¡¡the¡¡puter¡¡sees¡¡numbers¡¡as¡¡binary£»¡¡with¡¡only¡¡two¡¡unique¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡identifiers¡£¡¡Thus£»¡¡whole¡¡numbers¡¡could¡¡be¡¡viewed¡¡as¡¡arrays¡¡of¡¡Boolean¡¡values£»¡¡as¡¡there¡¡are¡¡only¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡two¡¡valid¡¡values£º¡¡1¡¡and¡¡0¡¡or¡¡True¡¡and¡¡¡¡False¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡look¡¡at¡¡example¡¡of¡¡using¡¡the¡¡bitwise¡¡operators¡£¡¡Say¡¡that¡¡you¡¡want¡¡to¡¡know¡¡whether¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡person¡¡is¡¡tall£»¡¡wears¡¡hats£»¡¡and¡¡runs¡¡slowly¡£¡¡Using¡¡Boolean¡¡data¡¡members£»¡¡you¡¡would¡¡write¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡following¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡PersonWithAttributes¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡IsPersonTall¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡WearsHats¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡RunsSlowly¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡

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

Äã¿ÉÄÜϲ»¶µÄ