Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to re-factor
Message
From
15/02/2013 05:53:51
 
 
To
15/02/2013 05:46:36
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:
01566124
Views:
53
>>>No, it has its place, I guess, but it could just as easily have returned an object with 2 properties.
>>
>>Don't see that as being so elegant. Compare:
internal class Program
>>  {
>>    private static void Main()
>>    {
>>      string s = "123";
>>      int i;
>>      if (!int.TryParse(s, out i))
>>      {
>>        //error
>>      }
>>
>>      TryParseReturnObject ret = JohnsTryParse(s);
>>      if (!ret.success)
>>      {
>>        //Error
>>      }
>>      i = ret.result;
>>    }
>>
>>    private static TryParseReturnObject JohnsTryParse(string s)
>>    {
>>      int result;
>>      bool success = int.TryParse(s, out result);
>>      return new TryParseReturnObject{result = result,success = success};
>>    }
>>  }
>>
>>  public class TryParseReturnObject
>>  {
>>    public int result { get; set; }
>>    public bool success { get; set; }
>>  }
>
>
>The Match object returned for RegEx uses your 'TryParseReturnObject' technique
>http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.aspx
>
>You get a Match back - and then you test its Success property

True but the Match class has to encapsulate many more properties.
>
>
>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......
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform