Access modifiers are not allowed on static constructors.

Question

Tuesday, December 8, 2009 8:44 PM

Hello,

I have the following:

  public class ValidatorFactory : IValidatorFactory {

    private static Dictionary<Type, IValidator> _validators = new Dictionary<Type, IValidator>();

    public static ValidatorFactory() {
      _validators.Add(typeof(AssetForm), new AssetFormValidator());
      _validators.Add(typeof(HomeContact), new HomeContactValidator());
      _validators.Add(typeof(UserSignIn), new UserSignInValidator());
    } // ValidatorFactory

    public IValidator<T> GetValidator<T>() {
      return (IValidator<T>)GetValidator(typeof(T));
    } // GetValidator

    public IValidator GetValidator(Type type) {
      IValidator validator;
      if (_validators.TryGetValue(type, out validator))
        return validator;
      return null;
    } // GetValidator

  } // ValidatorFactory

And I get the error:

'Helpers.ValidatorFactory.ValidatorFactory()': access modifiers are not allowed on static constructors.

What am I missing?

Thanks,
Miguel

All replies (2)

Tuesday, December 8, 2009 8:49 PM âś…Answered

You have a constructor that is both public and static.

public static ValidatorFactory() {
      _validators.Add(typeof(AssetForm), new AssetFormValidator());
      _validators.Add(typeof(HomeContact), new HomeContactValidator());
      _validators.Add(typeof(UserSignIn), new UserSignInValidator());
    } // ValidatorFactory

You need to make it either just public or just static. (The static constructor is called once when the class is first accessed. A non static constructor would be used to create instances of the class.)

-Jeff


Tuesday, December 8, 2009 9:14 PM

Thank You.