Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Best way to re-factor
Message
De
15/02/2013 05:46:36
 
 
À
14/02/2013 13:20:08
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01565967
Message ID:
01566123
Vues:
72
>>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


Personally I'm fine with TryXXX() methods - the reason they are there is to avoid an exception ( try/catch)
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform