This topic has not yet been rated - Rate this topic

ValidationResult Class

Represents a container for the results of a validation request.

System.Object
  System.ComponentModel.DataAnnotations.ValidationResult

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)
public class ValidationResult

The ValidationResult type exposes the following members.

  Name Description
Public method ValidationResult(String) Initializes a new instance of the ValidationResult class by using an error message.
Protected method ValidationResult(ValidationResult) Initializes a new instance of the ValidationResult class by using a ValidationResult object.
Public method ValidationResult(String, IEnumerable<String>) Initializes a new instance of the ValidationResult class by using an error message and a list of members that have validation errors.
Top
  Name Description
Public property ErrorMessage Gets the error message for the validation.
Public property MemberNames Gets the collection of member names that indicate which fields have validation errors.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public field Static member Success true if validation was successful; otherwise, false.
Top

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
VR Follows Guidelines
VR is not violating any guidelines here.  The guidelines do not say anywhere that you should never expose a public field (FxCop: 4.3.5).  The guidelines specify that if you need to expose a constant value (such as Success or Empty or Default) then you should prefer a static readonly field to a constant field (5.5).  The reasoning is beyond this scope.  Nevertheless this is the preferred approach to defining a constant object.  A property would provide no benefits over a readonly field in this case because the returned object is immutable and cannot be set.  The only benefit a property would give is that you could run some code when the property is accessed but what code would you run?  The only reasonable code is to initialize the field but since it is static readonly this only happens once and can be easily handled by the static ctor.  Maintainability-wise, since it is a static readonly field, the initialization can be modified in a future version and existing code is unaffected.  This is, perhaps, the biggest argument in favor of properties over fields but, again, in this case you can do so in the static ctor to accomplish the same behavior.
ValidationResult Design?

Why does Microsoft seem to violate its own espoused best practices for class design by exposing a public field? Why is Success a field when it could have been a property? I also see magic strings everywhere in System.ComponentModel.Design namespace arguments and usage. As a developer who is always trying to improve, it becomes very confusing to me when Microsoft constantly violates FxCop rules in its own libraries. It seems to be getting worse lately which sends conflicting messages.