What is wrong with my Syntax Here?

What is wrong with my Syntax Here?

Old forum URL: forums.lhotka.net/forums/t/7996.aspx


SouthSpawn posted on Sunday, November 15, 2009

protected override void AddBusinessRules()
{

      //It is complaining about the two lines below.
      //I am pretty new to c# coding.

      ValidationRules.AddRule<
Member>(PasswordValidationRule<Member>, PasswordProperty);
      ValidationRules.AddRule<
Member>(PasswordVerifyValidationRule<Member>, PasswordProperty);
}

private static bool PasswordValidationRule<T>(T target, Validation.RuleArgs e) where T : Member
{

      if (target.Password != target.PasswordVerify)
      {

            e.Description = "Passwords do not match.";

            return false;

      }

      return true;
}

private static bool PasswordVerifyValidationRule<T>(Member target, Validation.RuleArgs e) where T : Member
{

      if (target.Password != target.PasswordVerify)
      {
            e.Description =
"Password and password verify do not match.";

            return false;

        }

         return true;
}

Visual Studio is complaining here.

rsbaker0 replied on Sunday, November 15, 2009

Remove the T (and angle brackets) from your validation rule function definitions.

The generic argument when adding the rule specifies the type of the first argument to your rule method. The rule method itself need not be declared as generic.

SouthSpawn replied on Sunday, November 15, 2009

Are you saying like this?
 

private static bool PasswordValidationRule(T target, Validation.RuleArgs e) where T : Member


That doesn't work.


Can you show me an example?

RockfordLhotka replied on Monday, November 16, 2009

No, your rule method was (I think) fine. It is the AddRules() call that's the problem.

ValidationRules.AddRule<ObjectType>(RuleMethod, PropertyInfo);

Don't put the generic type on the rule method name here.

JonnyBee replied on Monday, November 16, 2009

Try this syntax;

      ValidationRules.AddRule<Member>(PasswordValidationRule, PasswordProperty);
      ValidationRules.AddRule<
Member>(PasswordVerifyValidationRule, PasswordProperty);

rsbaker0 replied on Monday, November 16, 2009

SouthSpawn:
Are you saying like this?

 


private static bool PasswordValidationRule(T target, Validation.RuleArgs e) where T : Member



That doesn't work.



Can you show me an example?



No, this:

private static bool PasswordValidationRule(Member target, Validation.RuleArgs e)
{
...
}

Also remove the second generic reference when you add the rule: (sorry, I can't get the angle brackets to work correctly in this forum editor -- use them instead of [ ])

ValidationRules.AddRule[Member](PasswordValidationRule, PasswordProperty);

Copyright (c) Marimer LLC