none
Validation for different types in C#

    Question

  • Validation for different types in C#
    I want to have required validation for different types of variables i.e. integer, DateTime, string, byte, short  in C# (not the requiredfieldvalidation), basically I will receive these variables from a web service and then I will validate them, so can you suggest me the best approach for their validations.
    e.g: for string I am doing like:
    if (!(shopName.Length > 0))
            throw new Exception("Shop Name is Empty");
    Thanks!! MCP
    Thursday, October 08, 2009 10:03 PM

Answers

  • You can validate all of your numbers using the same mechanism:

    if (myInt < -10 || myInt > 10)
       throw new ArgumentException("myInt should be between -10 and 10", "myInt");

    However, there are tools to make this cleaner.  For example, CuttingEdge.Conditions provides a nice fluent-style interface for validation, with strongly typed conditions depending on the type:

       Condition.Requires(myInt, "myInt").IsInRange(-10, 10);
       Condition.Requires(stopName, "shopName").IsNotEmpty();
    // ...


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Thursday, October 08, 2009 11:01 PM
    Moderator
  • Just a pet peeve but don't use not logic when you don't have to. The line

    if (!(shopName.Length > 0)) ...
    should be

    if (shopName.Length == 0) ...
    It's easier to understand that way.

    Ron Whittle - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Thursday, October 08, 2009 11:40 PM
  • Hi,

    Also maybe not a problem in your specific case, but be careful with the line

    if (!(shopName.Length > 0))

    As this will cause a null reference exception to be thrown if the string is null. You either need to use

    if (String.IsNullOrEmpty(shopName.Length)) //Handles both null and empty length

    or something equivalent such as

    if (shopName == null || shopName.Length == 0)

    or

    if (shopName == null)  throw new ArgumentNullException("shopName");
    if (shopName.Length == 0) throw new ArgumentException("Argument cannot be blank.", "shopName");

    Finally, if you like Reed's suggestion then you might want to check out Microsoft Code Contracts which work in a similar style to what he suggests but may or may not be appropriate for your particular problem.

    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Friday, October 09, 2009 2:35 AM

All replies

  • You can validate all of your numbers using the same mechanism:

    if (myInt < -10 || myInt > 10)
       throw new ArgumentException("myInt should be between -10 and 10", "myInt");

    However, there are tools to make this cleaner.  For example, CuttingEdge.Conditions provides a nice fluent-style interface for validation, with strongly typed conditions depending on the type:

       Condition.Requires(myInt, "myInt").IsInRange(-10, 10);
       Condition.Requires(stopName, "shopName").IsNotEmpty();
    // ...


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Thursday, October 08, 2009 11:01 PM
    Moderator
  • Just a pet peeve but don't use not logic when you don't have to. The line

    if (!(shopName.Length > 0)) ...
    should be

    if (shopName.Length == 0) ...
    It's easier to understand that way.

    Ron Whittle - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Thursday, October 08, 2009 11:40 PM
  • Hi,

    Also maybe not a problem in your specific case, but be careful with the line

    if (!(shopName.Length > 0))

    As this will cause a null reference exception to be thrown if the string is null. You either need to use

    if (String.IsNullOrEmpty(shopName.Length)) //Handles both null and empty length

    or something equivalent such as

    if (shopName == null || shopName.Length == 0)

    or

    if (shopName == null)  throw new ArgumentNullException("shopName");
    if (shopName.Length == 0) throw new ArgumentException("Argument cannot be blank.", "shopName");

    Finally, if you like Reed's suggestion then you might want to check out Microsoft Code Contracts which work in a similar style to what he suggests but may or may not be appropriate for your particular problem.

    • Marked as answer by Roahn Luo Wednesday, October 14, 2009 7:23 AM
    Friday, October 09, 2009 2:35 AM