ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
¸»Ê¿¿µÐ¡ËµÍø ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

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

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡



alphabetical¡¡order¡£¡¡The¡¡way¡¡that¡¡the¡¡Order¡¡By¡¡works¡¡is¡¡that¡¡the¡¡¡¡value¡¡of¡¡the¡¡variable¡¡is¡¡pared£»¡¡¡¡

rather¡¡than¡¡the¡¡actual¡¡variable¡£If¡¡you¡¡want¡¡to¡¡sort¡¡in¡¡reverse¡¡order£»¡¡you¡¡can¡¡use¡¡the¡¡keyword¡¡¡¡

Descending£»¡¡as¡¡follows£º¡¡



Dim¡¡sortedWords¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡From¡¡w¡¡In¡¡words¡¡_¡¡

¡¡¡¡¡¡¡¡Order¡¡By¡¡w¡¡Descending¡¡_¡¡

¡¡¡¡¡¡¡¡Select¡¡w¡¡



¡¡¡¡¡¡¡¡¡¡This¡¡approach¡¡allows¡¡you¡¡to¡¡perform¡¡sorts¡¡according¡¡to¡¡other¡¡values¡£¡¡For¡¡example£»¡¡you¡¡¡¡

could¡¡sort¡¡by¡¡the¡¡length¡¡of¡¡word£»¡¡like¡¡this£º¡¡



Dim¡¡sortedWords¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡From¡¡w¡¡In¡¡words¡¡_¡¡

¡¡¡¡¡¡¡¡Order¡¡By¡¡w¡£Length¡¡_¡¡

¡¡¡¡¡¡¡¡Select¡¡w¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡434¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Order¡¡By¡¡queries¡¡for¡¡the¡¡value¡¡of¡¡w¡£Length£»¡¡which¡¡returns¡¡a¡¡number¡£¡¡If¡¡that¡¡number¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡happens¡¡to¡¡be¡¡longer¡¡or¡¡shorter¡¡than¡¡another¡¡word£»¡¡it¡¡is¡¡placed¡¡after¡¡or¡¡before¡¡the¡¡other¡¡word¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡could¡¡also¡¡sort¡¡according¡¡to¡¡multiple¡¡criteria¡£¡¡For¡¡example£»¡¡you¡¡could¡¡sort¡¡the¡¡words¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡alphabetically¡¡and¡¡then¡¡by¡¡length£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡sortedWords¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡From¡¡w¡¡In¡¡words¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Order¡¡By¡¡w£»¡¡w¡£Length¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select¡¡w¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡sort¡¡by¡¡multiple¡¡criteria£»¡¡append¡¡them¡¡to¡¡the¡¡Order¡¡By¡¡keywords£»¡¡each¡¡separated¡¡by¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡ma¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡alphabetical¡¡and¡¡length¡¡sort¡¡is¡¡fruitless£»¡¡because¡¡when¡¡you¡¡sort¡¡alphabetically£»¡¡you¡¡will¡¡auto

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡matically¡¡sort¡¡by¡¡length¡£¡¡The¡¡example¡¡is¡¡just¡¡for¡¡illustrative¡¡purposes¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Performing¡¡Set¡¡Operations¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡last¡¡major¡¡topic¡¡that¡¡you¡¡need¡¡to¡¡know¡¡about¡¡when¡¡using¡¡LINQ¡¡is¡¡the¡¡ability¡¡to¡¡perform¡¡set¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡operations¡¡on¡¡a¡¡collection¡£¡¡The¡¡major¡¡downside£»¡¡however£»¡¡to¡¡performing¡¡set¡¡operations¡¡is¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡must¡¡use¡¡the¡¡methods£»¡¡and¡¡at¡¡the¡¡time¡¡of¡¡this¡¡writing£»¡¡no¡¡LINQ¡¡mand¡¡syntax¡¡existed¡¡for¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡set¡¡operations¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Knowing¡¡about¡¡set¡¡operations¡¡is¡¡useful¡¡because¡¡they¡¡enable¡¡you¡¡to¡¡sort¡¡and¡¡organize¡¡multiple¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡result¡¡sets¡£¡¡The¡¡examples¡¡in¡¡this¡¡section¡¡involve¡¡the¡¡Customer¡¡type¡¡again¡£¡¡However£»¡¡to¡¡make¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sure¡¡that¡¡the¡¡set¡¡operations¡¡function¡¡properly£»¡¡you¡¡need¡¡to¡¡implement¡¡the¡¡Equals£¨£©¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡GetHashCode£¨£©¡¡methods£»¡¡like¡¡this£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Friend¡¡Class¡¡Customer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡Equals£¨ByVal¡¡obj¡¡As¡¡Object£©¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡TypeOf¡¡obj¡¡Is¡¡Customer¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡otherObj¡¡As¡¡Customer¡¡=¡¡TryCast£¨obj£»¡¡Customer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡otherObj¡£Identifier¡£pareTo£¨Identifier£©¡¡=¡¡0¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡True¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡GetHashCode£¨£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Identifier¡£GetHashCode£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡ToString£¨£©¡¡As¡¡String¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡String¡£Concat£¨New¡¡Object£¨£©¡¡_¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£û¡¨Identifier¡¡£¨¡¨£»¡¡Me¡£Identifier£»¡¡¡¨£©¡¡Points¡¡£¨¡¨£»¡¡Me¡£Points£»¡¡¡¨£©¡¨£ý£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡435¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

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



¡¡¡¡¡¡¡¡Public¡¡Identifier¡¡As¡¡String¡¡

¡¡¡¡¡¡¡¡Public¡¡Points¡¡As¡¡Integer¡¡

End¡¡Class¡¡



¡öNote¡¡¡¡The¡¡GetHashCode£¨£©¡¡implementation¡¡here¡¡is¡¡rudimentary¡£¡¡In¡¡the¡¡source¡¡code¡¡that¡¡es¡¡with¡¡this¡¡¡¡

book£»¡¡you¡¡will¡¡find¡¡a¡¡GetHashCode¡¡library¡¡class£»¡¡which¡¡makes¡¡it¡¡simpler¡¡to¡¡implement¡¡GetHashCode£¨£©¡£¡¡The¡¡¡¡

source¡¡code¡¡is¡¡in¡¡the¡¡project¡¡ServerSideSpreadsheet/Devspace¡£Trader¡£mon/Automators¡£¡¡



¡¡¡¡¡¡¡¡¡¡Look¡¡at¡¡how¡¡GetHashCode£¨£©¡¡and¡¡¡¡Equals£¨£©¡¡are¡¡implemented¡£¡¡Notice¡¡that¡¡the¡¡¡¡points¡¡data¡¡¡¡

member¡¡is¡¡ignored¡£¡¡In¡¡the¡¡case¡¡of¡¡a¡¡customer£»¡¡this¡¡is¡¡acceptable£»¡¡because¡¡a¡¡customer¡¡with¡¡iden

tical¡¡identifiers¡¡but¡¡unequal¡¡points¡¡does¡¡not¡¡imply¡¡two¡¡separate¡¡customers¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Implementing¡¡Equals£¨£©¡¡and¡¡GetHashCode£¨£©¡¡for¡¡custom¡¡types¡¡is¡¡absolutely¡¡imperative£»¡¡because¡¡¡¡

the¡¡set¡¡operations¡¡use¡¡that¡¡information¡¡to¡¡determine¡¡whether¡¡two¡¡objects¡¡are¡¡identical¡£¡¡If¡¡you¡¡¡¡

don¡¯t¡¡implement¡¡either¡¡method£»¡¡the¡¡set¡¡operations¡¡will¡¡use¡¡the¡¡default¡¡implementations¡¡of¡¡¡¡

Equals£¨£©¡¡and¡¡GetHashCode£¨£©£»¡¡which¡¡are¡¡inplete¡¡and¡¡will¡¡give¡¡you¡¡the¡¡wrong¡¡results¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡next¡¡step¡¡is¡¡to¡¡create¡¡two¡¡separate¡¡lists¡¡of¡¡customers¡£¡¡In¡¡this¡¡example£»¡¡both¡¡lists¡¡contain¡¡the¡¡¡¡

same¡¡valued¡¡customer¡£¡¡Realize¡¡that¡¡the¡¡identical¡¡customer¡¡is¡¡not¡¡the¡¡same¡¡object¡¡instance£»¡¡but¡¡¡¡

contains¡¡the¡¡same¡¡values¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡customers1¡¡As¡¡Customer£¨£©¡¡=¡¡New¡¡Customer£¨£©¡¡£û¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Customer£¨£©¡¡With¡¡£û¡£Identifier¡¡=¡¡¡¨Person¡¡1¡¨£»¡¡¡£Points¡¡=¡¡0£ý£»¡¡_¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡customers2¡¡As¡¡Customer£¨£©¡¡=¡¡New¡¡Customer£¨£©¡¡£û¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Customer£¨£©¡¡With¡¡£û¡£Identifier¡¡=¡¡¡¨Person¡¡3¡¨£»¡¡¡£Points¡¡=¡¡0£ý£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡E
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨11£© ²È£¨11£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡