ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ21²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Germany£»¡¡Switzerland£»¡¡and¡¡Austria¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Setting¡¡Culture¡¡and¡¡Language¡¡in¡¡Windows¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Windows¡¡operating¡¡system¡¡allows¡¡you¡¡to¡¡set¡¡the¡¡culture¡¡and¡¡language¡¡of¡¡your¡¡puter£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡regardless¡¡of¡¡the¡¡language¡¡in¡¡which¡¡Windows¡¡is¡¡operating¡£¡¡Figure¡¡3¡12¡¡shows¡¡an¡¡example¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡example¡¡in¡¡Figure¡¡3¡12¡¡is¡¡running¡¡a¡¡German¡¡version¡¡of¡¡Windows¡¡in¡¡Switzerland¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡language¡¡is¡¡English£»¡¡and¡¡the¡¡culture¡¡is¡¡Canada¡£¡¡It¡¡would¡¡seem¡¡that¡¡Windows¡¡would¡¡get¡¡confused£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡but¡¡in¡¡fact£»¡¡if¡¡you¡¡write¡¡your¡¡¡¡application¡¡properly£»¡¡multilanguage¡¡support¡¡is¡¡simple¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡91¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡69¡¡
Figure¡¡3¡12¡£¡¡Setting¡¡the¡¡culture¡¡and¡¡language¡¡in¡¡Windows¡¡
Parsing¡¡and¡¡Processing¡¡Numbers¡¡
The¡¡culture¡¡and¡¡country¡¡bee¡¡important¡¡when¡¡interacting¡¡with¡¡numbers¡¡and¡¡dates¡¡that¡¡¡¡
are¡¡stored¡¡as¡¡strings¡£¡¡Imagine¡¡retrieving¡¡a¡¡string¡¡buffer¡¡with¡¡an¡¡embedded¡¡number¡¡and¡¡then¡¡¡¡
attempting¡¡to¡¡perform¡¡an¡¡addition£»¡¡as¡¡illustrated¡¡by¡¡Figure¡¡3¡13¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Variables¡¡a¡¡and¡¡b¡¡are¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡assigned¡¡buffers¡¡that¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡represent¡¡numbers¡¡
¡¡Dim¡¡a¡¡As¡¡String¡¡=¡¡¡¨1¡¨¡¡
Dim¡¡b¡¡As¡¡String¡¡=¡¡¡¨2¡¨¡¡
Dim¡¡c¡¡As¡¡String¡¡=¡¡a¡¡£«¡¡b¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡two¡¡buffers¡¡is¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡concatenation¡¡operation¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Variable¡¡c¡¡contains¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡value¡¡12¡¡
Figure¡¡3¡13¡£¡¡Performing¡¡arithmetic¡¡on¡¡numbers¡¡represented¡¡as¡¡strings¡¡can¡¡lead¡¡to¡¡unexpected¡¡results¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡92¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
70¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡numbers¡¡is¡¡performing¡¡a¡¡mathematical¡¡operation¡£¡¡When¡¡the¡¡add¡¡operation¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡performed¡¡on¡¡strings£»¡¡it¡¡always¡¡results¡¡in¡¡a¡¡buffer¡¡concatenation¡£¡¡The¡¡add¡¡operation¡¡is¡¡a¡¡very¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡simple¡¡way¡¡to¡¡concatenate¡¡string¡¡buffers¡¡together¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡concatenation¡¡is¡¡not¡¡the¡¡aim¡¡of¡¡the¡¡example¡£¡¡The¡¡aim¡¡is¡¡to¡¡treat¡¡the¡¡strings¡¡as¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡numbers¡¡and¡¡then¡¡add¡¡the¡¡numbers¡¡so¡¡that¡¡c¡¡contains¡¡the¡¡value¡¡¡¡3¡¡£¨1¡¡£«¡¡2¡¡=¡¡3£©¡£¡¡The¡¡rewritten¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡version¡¡of¡¡the¡¡example¡¡is¡¡shown¡¡in¡¡Figure¡¡3¡14¡£¡¡This¡¡code¡¡parses¡¡a¡¡string¡¡into¡¡an¡¡integer¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer¡¡is¡¡a¡¡value¡¡type£»¡¡but¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡recognizes¡¡the¡¡methods¡¡associated¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡the¡¡value¡¡types¡¡that¡¡can¡¡be¡¡used¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡parse¡¡string¡¡buffers¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡a¡¡As¡¡Integer¡¡=¡¡Integer¡£Parse£¨¡¨1¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡b¡¡As¡¡Integer¡¡=¡¡Integer¡£Parse£¨¡¨2¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡c¡¡As¡¡Integer¡¡=¡¡a¡¡£«¡¡b¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Variable¡¡c¡¡contains¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Variables¡¡a¡¡and¡¡b¡¡are¡¡assigned¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡value¡¡3¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡values¡¡that¡¡represent¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡parsed¡¡string¡¡buffers¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡3¡14¡£¡¡Parsing¡¡strings¡¡into¡¡integers¡¡before¡¡doing¡¡the¡¡arithmetic¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡type¡¡Integer¡¡has¡¡a¡¡¡¡Parse£¨£©¡¡method¡¡that¡¡can¡¡be¡¡used¡¡to¡¡turn¡¡a¡¡string¡¡into¡¡an¡¡integer¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡parsing¡¡works¡¡only¡¡if¡¡the¡¡buffer¡¡is¡¡a¡¡valid¡¡number¡£¡¡If¡¡the¡¡buffer¡¡contains¡¡letters¡¡or¡¡an¡¡invalid¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number£»¡¡an¡¡error¡¡will¡¡be¡¡generated¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡code¡¡can¡¡cope¡¡with¡¡a¡¡failed¡¡string¡¡conversion£»¡¡the¡¡solution¡¡used¡¡by¡¡the¡¡parsing¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡is¡¡to¡¡generate¡¡an¡¡exception¡¡that¡¡a¡¡program¡¡could¡¡process¡£¡¡Alternatively£»¡¡a¡¡fail¡safe¡¡way¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡parse¡¡a¡¡number¡¡is¡¡to¡¡use¡¡TryParse£¨£©£»¡¡as¡¡in¡¡the¡¡following¡¡example¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡Integer¡£TryParse£¨¡¨1¡¨£»¡¡value£©¡¡Then¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡TryParse£¨£©¡¡method¡¡does¡¡not¡¡return¡¡an¡¡integer¡¡value£»¡¡but¡¡instead¡¡returns¡¡a¡¡¡¡Boolean¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡flag£»¡¡indicating¡¡whether¡¡the¡¡buffer¡¡could¡¡be¡¡parsed¡£¡¡If¡¡the¡¡return¡¡value¡¡is¡¡True£»¡¡then¡¡the¡¡buffer¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡could¡¡be¡¡parsed£»¡¡and¡¡the¡¡result¡¡is¡¡stored¡¡in¡¡the¡¡parameter¡¡value¡£¡¡You¡¡can¡¡parse¡¡other¡¡number¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡¡using¡¡the¡¡same¡¡techniques¡¡£¨for¡¡example£»¡¡Single¡£TryParse£¨£©£©¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡There¡¡are¡¡more¡¡variations¡¡in¡¡how¡¡a¡¡number¡¡can¡¡be¡¡parsed¡£¡¡For¡¡example£»¡¡how¡¡would¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number¡¡100¡¡be¡¡parsed£»¡¡if¡¡the¡¡number¡¡100¡¡is¡¡hexadecimal£¿¡¡£¨puters¡¡use¡¡hexadecimal¡¡numbers¡££©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Here¡¡is¡¡an¡¡example¡¡of¡¡hexadecimal¡¡conversion£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡System¡£Globalization¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡ParseHexadecimal£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡Integer=¡¡Integer¡£Parse£¨¡¨100¡¨£»¡¡NumberStyles¡£HexNumber£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡93¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡71¡¡
¡¡¡¡¡¡¡¡¡¡This¡¡example¡¡uses¡¡a¡¡variant¡¡of¡¡¡¡Parse£¨£©£»¡¡which¡¡has¡¡an¡¡additional¡¡second¡¡parameter¡¡that¡¡¡¡
represents¡¡the¡¡format¡¡of¡¡the¡¡number¡£¡¡In¡¡this¡¡case£»¡¡the¡¡second¡¡parameter¡¡indicates¡¡that¡¡the¡¡¡¡
format¡¡of¡¡the¡¡number¡¡is¡¡hexadecimal¡¡£¨NumberStyles¡£HexNumber£»¡¡from¡¡the¡¡System¡£Globalization¡¡¡¡
namespace£©¡£¡¡¡¡
¡öNote¡¡¡¡¡¡If¡¡you¡¡are¡¡wondering¡¡how¡¡100¡¡maps¡¡to¡¡256¡¡at¡¡the¡¡hex¡¡level£»¡¡use¡¡the¡¡calculator¡¡that¡¡es¡¡with¡¡the¡¡¡¡
Windows¡¡operating¡¡system¡£¡¡Switch¡¡the¡¡calculator¡¡to¡¡scientific¡¡view¡¡£¨View¡¡¡¡Scientific£©¡£¡¡Click¡¡the¡¡Hex¡¡radio¡¡¡¡
button£»¡¡enter¡¡the¡¡number¡¡100£»¡¡and¡¡then¡¡click¡¡the¡¡Dec¡¡radio¡¡button¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡enumeration¡¡NumberStyles¡¡has¡¡other¡¡values¡¡that¡¡can¡¡be¡¡used¡¡to¡¡parse¡¡numbers¡¡according¡¡¡¡
to¡¡other¡¡rules¡£¡¡For¡¡example£»¡¡some¡¡rules¡¡handle¡¡the¡¡use¡¡of¡¡parentheses¡¡surrounding¡¡a¡¡number¡¡¡¡
to¡¡indicate¡¡a¡¡negative¡¡value¡£¡¡Other¡¡rules¡¡deal¡¡with¡¡whitespace¡£¡¡Here¡¡is¡¡an¡¡example£º¡¡
Public¡¡Sub¡¡TestParseNegativeValue£¨£©¡¡
¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡Integer¡¡=¡¡Integer¡£Parse£¨¡¡¡¨¡¡£¨10£©¡¡¡¨£»¡¡_¡¡
¡¡¡¡¡¡¡¡NumberStyles¡£AllowParentheses¡¡Or¡¡_¡¡¡¡
¡¡¡¡¡¡¡¡NumberStyles¡£AllowLeadingWhite¡¡Or¡¡_¡¡
¡¡¡¡¡¡¡¡NumberStyles¡£AllowTrailingWhite£©¡¡
End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡number¡¡¡¨¡¡¡¡£¨10£©¡¡¡¡¡¨¡¡in¡¡this¡¡example¡¡is¡¡plicated¡¡in¡¡that¡¡it¡¡has¡¡whitespace¡¡and¡¡paren
theses¡£¡¡Attempting¡¡to¡¡parse¡¡the¡¡number¡¡using¡¡Parse£¨£©¡¡without¡¡using¡¡any¡¡of¡¡the¡¡¡¡NumberStyles¡¡¡¡
enumerated¡¡values¡¡will¡¡not¡¡work¡£¡¡The¡¡enumeration¡¡AllowParentheses¡¡processes¡¡the¡¡parentheses£»¡¡¡¡
AllowLeadingWhite¡¡ignores¡¡the¡¡leading¡¡spaces£»¡¡and¡¡AllowTrailingWhite¡¡ignores¡¡the¡¡trailing¡¡¡¡
spaces¡£¡¡Then£»¡¡when¡¡the¡¡buffer¡¡has¡¡been¡¡processed£»¡¡a¡¡value¡¡of¡¡¨C10¡¡will¡¡be¡¡stored¡¡in¡¡the¡¡variable¡¡¡¡
value¡£¡¡
¡¡¡¡¡¡¡¡¡¡Other¡¡NumberStyles¡¡enumerated¡¡values¡¡allow¡¡you¡¡to¡¡process¡¡decimal¡¡points¡¡for¡¡fractional¡¡¡¡
numbers£»¡¡positive¡¡or¡¡negative¡¡numbers£»¡¡and¡¡so¡¡on¡£¡¡This¡¡then¡¡raises¡¡the¡¡topic¡¡of¡¡processing¡¡¡¡
numbers¡¡other¡¡than¡¡Integer¡£¡¡Each¡¡of¡¡the¡¡base¡¡data¡¡types£»¡¡such¡¡as¡¡Boolean£»¡¡Byte£»¡¡and¡¡Double£»¡¡¡¡
has¡¡associated¡¡Parse£¨£©¡¡and¡¡TryParse£¨£©¡¡methods¡£¡¡Additionally£»¡¡the¡¡method¡¡TryParse£¨£©¡¡can¡¡use¡¡¡¡
the¡¡NumberStyles¡¡enumeration¡£¡¡£¨See¡¡the¡¡MSDN¡¡documentation¡¡for¡¡details¡¡on¡¡the¡¡NumberStyles¡¡¡¡
enumerated¡¡values¡££©¡¡
¡¡¡¡¡¡¡¡¡¡Parsing¡¡integer¡¡values¡¡is¡¡the¡¡same£»¡¡regardless¡¡of¡¡the¡¡country¡£¡¡Parsing¡¡double¡¡values¡¡or¡¡dates¡¡¡¡
is¡¡not¡¡the¡¡same¡£¡¡Consider¡¡the¡¡following¡¡example£»¡¡which¡¡tries¡¡to¡¡parse¡¡a¡¡buffer¡¡that¡¡contains¡¡¡¡
decimal¡¡values¡£¡¡
Public¡¡Sub¡¡TestDoubleValue£¨£©¡¡
¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡Double¡¡=¡¡Double¡£Parse£¨¡¨1234¡£56¡¨£©¡¡
¡¡¡¡¡¡¡¡value¡¡=¡¡Double¡£Parse£¨¡¨1£»234¡£56¡¨£©¡¡
End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡example£»¡¡both¡¡uses¡¡of¡¡the¡¡¡¡Parse£¨£©¡¡method¡¡process¡¡the¡¡number¡¡1234¡£56¡£¡¡The¡¡first¡¡¡¡
Parse£¨£©¡¡method¡¡is¡¡a¡¡simple¡¡parse£»¡¡because¡¡it¡¡contains¡¡only¡¡a¡¡decimal¡¡point¡¡separating¡¡the¡¡¡¡
whole¡¡number¡¡from¡¡the¡¡partial¡¡number¡£¡¡The¡¡second¡¡Parse£¨£©¡¡method¡¡is¡¡more¡¡plicated¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡94¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
72¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡a¡¡ma¡¡is¡¡used¡¡to¡¡separate¡¡the¡¡thousands¡¡of¡¡the¡¡whole¡¡number¡£¡¡In¡¡both¡¡cases£»¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Parse£¨£©¡¡routines¡¡did¡¡not¡¡fail¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡test¡¡this¡¡code£»¡¡it¡¯s¡¡possible¡¡that¡¡an¡¡exception¡¡will¡¡be¡¡generated¡£¡¡In¡¡this¡¡case£»¡¡the¡¡culture¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡application¡¡is¡¡to¡¡blame¡£¡¡The¡¡numbers¡¡presented¡¡in¡¡the¡¡example¡¡are¡¡encoded¡¡using¡¡en¡CA£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡is¡¡English¡Canadian¡¡notation¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Working¡¡with¡¡Cultures¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡£»¡¡culture¡¡information¡¡is¡¡made¡¡up¡¡using¡¡two¡¡identifiers£º¡¡language¡¡and¡¡specialization¡£¡¡As¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡I¡¡mentioned¡¡earlier£»¡¡in¡¡Switzerland£»¡¡there¡¡are¡¡four¡¡spoken¡¡languages£»¡¡which¡¡means¡¡that¡¡there¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡four¡¡different¡¡ways¡¡of¡¡expressing¡¡a¡¡date£»¡¡time£»¡¡and¡¡currency¡£¡¡This¡¡does¡¡not¡¡mean¡¡that¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡date¡¡is¡¡different¡¡for¡¡German¡¡speakers¡¡and¡¡French¡¡speakers¡£¡¡The¡¡date¡¡format¡¡will¡¡be¡¡identical£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡but¡¡the¡¡words¡¡£¨Maerz¡¡or¡¡Mars¡¡for¡¡the¡¡month¡¡March£©¡¡will¡¡be¡¡different¡£¡¡The¡¡words¡¡for¡¡the¡¡date¡¡are¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡same¡¡in¡¡Austria£»¡¡Switzerland£»¡¡and¡¡Germany£»¡¡but¡¡the¡¡format¡¡is¡¡not¡¡identical¡£¡¡This¡¡means¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡multilanguage¡¡countries¡¡such¡¡as¡¡Canada¡¡£¨French¡¡and¡¡English£©¡¡and¡¡Luxembourg¡¡£¨French¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡German£©¡¡need¡¡to¡¡be¡¡able¡¡to¡¡process¡¡multiple¡¡encodings£»¡¡hence¡¡the¡¡need¡¡for¡¡the¡¡two¡¡identifiers¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡retrieve¡¡the¡¡current¡¡culture£»¡¡use¡¡the¡¡following¡¡code£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡info¡¡As¡¡System¡£Globalization¡£CultureInfo¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡System¡£Threading¡£Thread¡£CurrentThread¡£CurrentCulture¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Culture¡¡£¨¡¨¡¡&¡¡info¡£EnglishName¡¡&¡¡¡¨£©¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡property¡¡Thread¡£CurrentThread¡£CurrentCulture¡¡retrieves¡¡the¡¡culture¡¡information¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡associated¡¡with¡¡the¡¡currently¡¡executing¡¡thread¡£¡¡As¡¡a¡¡side¡¡note£»¡¡it¡¡is¡¡possible¡¡to¡¡associate¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡different¡¡threads¡¡with¡¡different¡¡cultural¡¡information¡£¡¡The¡¡property¡¡EnglishName¡¡generates¡¡an¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡English¡¡version¡¡of¡¡the¡¡culture¡¡information£»¡¡which¡¡would¡¡appear¡¡similar¡¡to¡¡the¡¡following£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Culture¡¡£¨English¡¡£¨Canada£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Consider¡¡the¡¡following¡¡number£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1£»234¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡number¡¡with¡¡an¡¡American¡¡or¡¡Canadian¡¡culture¡¡is¡¡one¡¡thousand¡¡two¡¡hundred¡¡thirty
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡four£»¡¡but¡¡with¡¡a¡¡German¡¡culture£»¡¡it¡¡is¡¡one¡¡point¡¡two¡¡three¡¡four¡¡£¨for¡¡those¡¡who¡¡do¡¡not¡¡know¡¡about¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡German¡¡formatting£»¡¡a¡¡ma¡¡is¡¡used¡¡as¡¡a¡¡decimal¡¡separator£»¡¡and¡¡a¡¡period¡¡is¡¡used¡¡as¡¡a¡¡thousands¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡separator£©¡£¡¡One¡¡way¡¡to¡¡change¡¡the¡¡culture¡¡is¡¡with¡¡the¡¡dialog¡¡box¡¡shown¡¡earlier¡¡in¡¡Figure¡¡3¡12¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡second¡¡way¡¡to¡¡change¡¡the¡¡culture¡¡is¡¡at¡¡a¡¡programmatic¡¡level£»¡¡as¡¡in¡¡this¡¡code£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡System¡£Threading¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thread¡£CurrentThread¡£CurrentCulture¡¡=¡¡new¡¡CultureInfo£¨¡¨en¡CA¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡a¡¡new¡¡instance¡¡of¡¡CultureInfo¡¡is¡¡created¡¡with¡¡the¡¡culture¡¡information¡¡en¡CA¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡is¡¡an¡¡example¡¡that¡¡processes¡¡a¡¡double¡¡number¡¡encoded¡¡using¡¡German¡¡formatting¡¡rules£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TestGermanParseNumber£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thread¡£CurrentThread¡£CurrentCulture¡¡=¡¡new¡¡CultureInfo£¨¡¨de¡DE¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡value¡¡As¡¡Double¡¡=¡¡Double¡£Parse£¨¡¨1£»234¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡example¡¡assigns¡¡the¡¡de¡DE¡¡culture¡¡information¡¡to¡¡the¡¡currently¡¡executing¡¡thread¡£¡¡Then¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whenever
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡