Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to re-factor
Message
From
15/02/2013 07:19:17
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01565967
Message ID:
01566126
Views:
56
>>
>>Personally I'm fine with TryXXX() methods - the reason they are there is to avoid an exception ( try/catch)
>
>Presumably the TryParse() method is actually implemented as a Parse() within a try/catch anyway. So not more efficient - just easier to use......

Didn't think that

but to put my mind at rest - had to figure that one out

bool Viv.TryDecide
{
compare Number.StringToNumber() and Number.TryStringToNumber()
}




(1) Int32.Parse
        public static int Parse(String s) {
            return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo); 
        }
Number.ParseInt32()
       internal unsafe static Int32 ParseInt32(String s, NumberStyles style, NumberFormatInfo info) {

            Byte * numberBufferBytes = stackalloc Byte[NumberBuffer.NumberBufferBytes];
            NumberBuffer number = new NumberBuffer(numberBufferBytes); 
            Int32 i = 0;
 
            StringToNumber(s, style, ref number, info, false); 

            if ((style & NumberStyles.AllowHexSpecifier) != 0) { 
                if (!HexNumberToInt32(ref number, ref i)) {
                    throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
                }
            } 
            else {
                if (!NumberToInt32(ref number, ref i)) { 
                    throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); 
                }
            } 
            return i;
        }
Number.StringToNumber
      private unsafe static void StringToNumber(String str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal) { 

            if (str == null) {
                throw new ArgumentNullException("String");
            } 
            Contract.EndContractBlock();
            Contract.Assert(info != null, ""); 
            fixed (char* stringPointer = str) { 
                char * p = stringPointer;
                if (!ParseNumber(ref p, options, ref number, null, info , parseDecimal) 
                    || (p - stringPointer < str.Length && !TrailingZeros(str, (int)(p - stringPointer)))) {
                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                }
            } 
        }
(2)
Int32.TryParse
       public static bool TryParse(String s, out Int32 result) { 
            return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); 
        }
Number.TryParseInt32
       internal unsafe static Boolean TryParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt, out Double result) {
            Byte * numberBufferBytes = stackalloc Byte[NumberBuffer.NumberBufferBytes]; 
            NumberBuffer number = new NumberBuffer(numberBufferBytes);
            result = 0;

 
            if (!TryStringToNumber(value, options, ref number, numfmt, false)) {
                return false; 
            } 
            if (!NumberBufferToDouble(number.PackForNative(), ref result)) {
                return false; 
            }
            return true;
        }
Number.TryStringToNumber()
      internal unsafe static Boolean TryStringToNumber(String str, NumberStyles options, ref NumberBuffer number, StringBuilder sb, NumberFormatInfo numfmt, Boolean parseDecimal) {
 
            if (str == null) {
                return false; 
            } 
            Contract.Assert(numfmt != null, "");
 
            fixed (char* stringPointer = str) {
                char * p = stringPointer;
                if (!ParseNumber(ref p, options, ref number, sb, numfmt, parseDecimal)
                    || (p - stringPointer < str.Length && !TrailingZeros(str, (int)(p - stringPointer)))) { 
                    return false;
                } 
            } 

            return true; 
        }
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform