ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡ÔĶÁ¹ý³Ì·¢ÏÖÈκδíÎóÇë¸æËßÎÒÃÇ£¬Ð»Ð»£¡£¡ ±¨¸æ´íÎó
¹·¹·Êé¼® ·µ»Ø±¾ÊéĿ¼ ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ½øÈëÊé°É ¼ÓÈëÊéÇ©

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ38ÕÂ

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




¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨0£© ²È£¨0£©
δÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡