0

I'm coding Entity Framework models and I want to validate incoming data for a given property against a predefined list of allowable values. Based on prior research, I decided the best way to do that is via a customized data annotation attribute, making sure that every property that needs this validation has an accompanying array of values that is passed to this attribute, called "[AllowableValue]"

So I have the following class:

public class className
{
    public int Id { get; set; }

    [Required]
    [AllowableValues(ListOfAllowableValues)]
    [MaxLength(2), MinLength(2)]
    public string propertyName { get; set; }

    [NotMapped]
    public string[] ListOfAllowableValues = new string[]
    {
        "00",
        "77",
        "ZZ"
    };

}

And the following Custom Attribute:

public class AllowableValues : ValidationAttribute
{
    string[] _list;

    public AllowableValues(string[] list)
    {
        _list = list;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (_list.Contains((string)value))
            return ValidationResult.Success;
        return new ValidationResult("Invalid value.");
    }
}

But, In Visual Studio, when I apply the [AllowableValues] attribute, it is giving me the error: "An object reference is required for the non-static field, method, or property 'className.ListOfAllowableValues.'

My definition calls for an array, and I'm passing it an array. Why is it asking for an object reference?

2
  • This was a little bit more informative than the first answer. Can you please move this comment down to the answers section so I can accept it? Commented Feb 3, 2017 at 16:18
  • Moved it, but am sorry that it doesn't really solve your problem. Commented Feb 3, 2017 at 16:26

2 Answers 2

2

Attribute declaration must be compile time constant.

So you're in a static context and have no instance of classname. But ListOfAllowableValues is not static but an instance member, so you cannot access it without a classname instance (that is what the error means by "object reference").

Unfortunatly I guess making ListOfAllowableValues static won't help as arguments to attributes must be compile time constant anyway and reference type values cannot be compile time constant.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This answer does give me a little better idea of why it's not possible, so I'm accepting this one. Thank you very much.
1

It is not possible. See here on SO

Can I initialize a C# attribute with an array or other variable number of arguments?

How to: MSDN CustomAttributes

Your only way is to do the following:

[Required]
[AllowableValues(new[]{ "00", "77", "ZZ"})]
[MaxLength(2), MinLength(2)]
public string propertyName { get; set; }

1 Comment

Useful information. But the requirements for this mean that the list of allowable values can get very large when applied to other properties. I was hoping to be able to pass an array to make it all more readable. It looks like I'm going to have to do this via controller code. Thank you though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.