Cannot convert from 'method group' error ???

Cannot convert from 'method group' error ???

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


david.wendelken posted on Monday, April 16, 2007

We're creating some new standard rules. (We cloned CommonRules into StdRules and started adding our own.  So, where you see StdRules, think CommonRules)

Here's the new rule we added. It's a generic test to see whether one date is less than or equal to another (i.e., EffectiveDt <= ObsoleteDt)

We're getting these three errors when we try to add it to a class like this:

ValidationRules.AddRule(StdRules.MinMaxDate
                                             ,new StdRules.MinMaxDateRuleArgs("EffDt", "EffDt", "ObsDt")
                                             );

  1. The best overloaded method match for 'Csla.Validation.ValidationRules.AddRule(Csla.Validation.RuleHandler, string)' has some invalid arguments
  2. Argument '1': cannot convert from 'method group' to 'Csla.Validation.RuleHandler'
  3. Argument '2': cannot convert from 'CslaSrd.Validation.StdRules.MinMaxDateRuleArgs' to 'string'

We don't have a clue as to what is going wrong.

 

 

 

#region MinMaxDate

public static bool MinMaxDate(object target, MinMaxDateRuleArgs e)
{

// Get the value of the specified property and use it for the starting date.
SmartDate
minDt = (SmartDate)Utilities.CallByName(target, e.MinDatePropertyName, CallType.Get);
// Get the value of the specified property and use it for the ending date.
SmartDate
maxDt = (SmartDate)Utilities.CallByName(target, e.MaxDatePropertyName, CallType.Get);

if (minDt > maxDt)
{
   try
  {
    e.Description =
string.Format(Resources.ruleMinMaxDate, e.MinDatePropertyName, e.MaxDatePropertyName);
  }
  catch
  {
    e.Description =
Resources.ruleMinMaxDate.ToString();
  }
 
return false;
}
else
{
  
return true;
}

}

/// <summary>
/// Custom <see cref="RuleArgs" /> object required by the
/// <see cref="MinMaxDate" /> rule method.
/// </summary>
public class MinMaxDateRuleArgs : RuleArgs
{
   private string _minDatePropertyName;
   private string _maxDatePropertyName;

/// <summary>
/// Get the name of the property that holds the min date value.
/// </summary>
public string MinDatePropertyName
{
 
get { return _minDatePropertyName; }
}

/// <summary>
/// Get the name of the property that holds the max date value.
/// </summary>
public string MaxDatePropertyName
{
 
get { return _maxDatePropertyName; }
}

/// <summary>
/// Create a new object to contain the arguments for a MinMaxDate rule.
/// </summary>
/// <param name="propertyName">Name of the property to validate.</param>
/// <param name="minDatePropertyName">Name of property containing min date to be checked.</param>
/// <param name="maxDatePropertyName">Name of property containing max date to be checked.</param>
public MinMaxDateRuleArgs
(
string propertyName, string minDatePropertyName, string maxDatePropertyName)
 
: base(propertyName
)
{
 
_minDatePropertyName = minDatePropertyName;
  _maxDatePropertyName = maxDatePropertyName;

}

/// <summary>
/// Return a string representation of the object.
/// </summary>
public override string ToString()
{
  return base.ToString() +"?minDatePropertyName=" + _minDatePropertyName
                                          + "&maxDatePropertyName=" + _maxDatePropertyName;
}

}

#endregion

david.wendelken replied on Monday, April 16, 2007

Forgot to mention that these are compilation errors, not runtime errors.

JoeFallon1 replied on Monday, April 16, 2007

I have a similar class in VB where I copied and then expanded on CommonRules.

All of my rules have code like this:

Public Function StringMaxLength(ByVal target As Object, ByVal e As RuleArgs) As Boolean
 
Dim ruleArg As MaxLengthRuleArgs = DirectCast(e, MaxLengthRuleArgs)

Notice that it uses e As RuleArgs.
I think your methods are using the subclass in the parameter list. That may not be allowed. Then the first line of code casts e to the correct subclass.

=====================================================

I wire up the rules in my BO like this:

ValidationRules.AddRule(AddressOf StringMaxLength, New MaxLengthRuleArgs("SomeProperty", 10))

Joe

 

RockfordLhotka replied on Monday, April 16, 2007

Yes, if you are going to use strong types for 'e' then you need to use the generic overloads of AddRule() and use generics through to your rule methods.

Hopefully this will change in .NET 3.5, where at least VB is getting relaxed delegate parameter calling (where they do overloading just like with regular methods), but today this doesn't exist and you need to take the generic route...

casbornsen replied on Tuesday, April 17, 2007

Hi guys.  My name is Charlie and I work with Dave.  Thanks for the help.  We went back and applied the generics and it worked great!  Better, since now it works for anything, not just SmartDates.

This is from our  RuleBusinessBase.  We add the rule once for Effective Date and once for Obsolete Date:

ValidationRules.AddRule<Object, StdRules.MinMaxRuleArgs>(StdRules.MinMax<SmartDate>
   ,
new StdRules.MinMaxRuleArgs("EffDt", "EffDt", "ObsDt"));
ValidationRules.AddRule<
Object, StdRules.MinMaxRuleArgs>(StdRules.MinMax<SmartDate>
   ,
new StdRules.MinMaxRuleArgs("ObsDt", "EffDt", "ObsDt"));

...

And this is the region in the StdRules class that Dave built based on Common Rules from CLSA 2.1:

#region MinMax

public
static bool MinMax<T>(object target, RuleArgs e) where T : IComparable

{
    // What should we call this instead of ee?  
   MinMaxRuleArgs
ee = (MinMaxRuleArgs)e;   
   T minValue = (T)Utilities.CallByName(target, ee.MinPropertyName, CallType.Get);
   T maxValue = (T)Utilities.CallByName(target, ee.MaxPropertyName, CallType.Get);
   if (minValue.CompareTo(maxValue) == 1)
   {
   try
      
{
         ee.Description = Resources.ruleMinMax.Replace("{minPropertyName}",ee.MinPropertyName);
         ee.Description  = ee.Description.Replace(
"{maxPropertyName}", ee.MaxPropertyName);
      }
   
catch
      
{
         ee.Description =
Resources.ruleMinMax.ToString();
      }
   
return false;
   }
   
else
   
{
      
return true;
   }
}

/// <summary>
/// Custom <see cref="RuleArgs" /> object required by the
/// <see cref="MinMax" /> rule method.
/// </summary>

public class MinMaxRuleArgs : RuleArgs
{
   
private string _minPropertyName;
   
private string _maxPropertyName;

   /// <summary>
   
/// Get the name of the property that holds the min value.
   
/// </summary>

   public string MinPropertyName
   {
      
get { return _minPropertyName; }
   }

   /// <summary>
   /// Get the name of the property that holds the max value.
   
/// </summary>

   public string MaxPropertyName
   {
      
get { return _maxPropertyName; }
   }

   /// <summary>
   
/// Create a new object.
   
/// </summary>
   
/// <param name="propertyName">Name of the property to validate.</param>
   
/// <param name="minPropertyName">Name of property containing min value to be checked.</param>
   
/// <param name="maxPropertyName">Name of property containing max value to be checked.</param>
   
   
public MinMaxRuleArgs(string propertyName, string minPropertyName, string maxPropertyName)
      :
base(propertyName)
   {
      _minPropertyName = minPropertyName;
      _maxPropertyName = maxPropertyName;
   }

   /// <summary>
   
/// Return a string representation of the object.
   
/// </summary>

   public override string ToString()
   {
      
return base.ToString() +"?minPropertyName=" + _minPropertyName + "&maxPropertyName=" +
         _maxPropertyName;
   }
}

#endregion

SonOfPirate replied on Thursday, June 07, 2007

Does anyone have an explanation of the "cannot convert from 'method group' to 'Csla.Validation.RuleHandler'.???

We have added a new validation rule specific to our class with no generics and the basic RuleArgs, similar to:

public static bool DateRequired(object sender, RuleArgs e)
{
}

When adding the rule to our BO, we are calling:

ValidationRules.AddRule(DateRequired, new Csla.Validation.RuleArgs("propertyName"));

Except we get the method group compiler error .  We can't figure out any explanation why this error should be occurring.  Any help is greatly appreciated.

 

Copyright (c) Marimer LLC