Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Workaround for accessing non-static property in a static context for Validation C#

Let’s say I have the class Wallet:

public class Wallet
{
       public double Min { get; set; }

       public double Max { get; set; }

       public double Value { get; set; }
}

And let’s say that the data is read from a database.

The user is able to modify Value from the UI.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to validate it somehow, so my initial thought was:

[Range(Min, Max, ErrorMessage = "The value is outside of bounds.")]
public double Value { get; set; }

But an error is thrown: Cannot access non-static property in a static context attribute.

Is there a workaround or do I have to create a custom validation?

Context: WPF MVVM application, C#

>Solution :

You could move that validation to the setter, and throw the exact validation exception that the attribute class throws if any validation erros occur.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading