Partilhar via


ValidationResult Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Contains the results of a validation request.

Inheritance Hierarchy

System.Object
  System.ComponentModel.DataAnnotations.ValidationResult

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
Public NotInheritable Class ValidationResult
public sealed class ValidationResult

The ValidationResult type exposes the following members.

Constructors

  Name Description
Public method ValidationResult(String) Initializes a new instance of the ValidationResult class with the specified error message.
Public method ValidationResult(String, IEnumerable<String>) Initializes a new instance of the ValidationResult class with the specified error message and a collection of member names that are associated with the validation result.

Top

Properties

  Name Description
Public property ErrorMessage Gets or sets the error message for the validation result.
Public property MemberNames Gets the collection of member names associated with the validation result.

Top

Methods

  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 the Object 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 value that represents the current validation result. (Overrides Object.ToString().)

Top

Fields

  Name Description
Public fieldStatic member Success A value that indicates the entity member successfully validated.

Top

Remarks

The ValidationResult class stores the outcome of a validation check. The IsValid and GetValidationResult methods return an instance of the ValidationResult class, which contains values to indicate whether the value of the entity member matches the validation attributes applied to that member.

If the value of the member successfully validates, the returned ValidationResult object equals the value of the Success field. To determine whether validation succeeded, you should check whether the returned object equals Success. If the value of the member does not validate, the returned ValidationResult object contains an error message and a collection of member names for the validation error, if they can be retrieved.

Examples

The following example shows how to return a validation result that indicates success or failure.

Imports System.ComponentModel.DataAnnotations

Public Class AWValidation
  Public Shared Function ValidateSalesPerson(salesPerson As String) As ValidationResult
    Dim isValid As Boolean

    ' Perform validation logic here and set isValid to true or false.

    If (IsValid) Then
      ValidateSalesPerson = ValidationResult.Success
    Else
      ValidateSalesPerson = New ValidationResult( _
          "The selected sales person is not available for this customer.")
    End If
    End Function

  Public Shared Function ValidateAddress(addressToValidate As CustomerAddress) As ValidationResult
    Dim isValid As Boolean

    ' Perform validation logic here and set isValid to true or false.

    If (IsValid) Then
      ValidateAddress = ValidationResult.Success
    Else
      ValidateAddress = New ValidationResult( _
          "The address for this customer does not match the required criteria.")
    End If
  End Function
End Class
using System.ComponentModel.DataAnnotations;

public class AWValidation
{
  public static ValidationResult ValidateSalesPerson(string salesPerson)
  {
    bool isValid;

    // Perform validation logic here and set isValid to true or false.

    if (isValid)
    {
      return ValidationResult.Success;
    }
    else
    {
      return new ValidationResult(
          "The selected sales person is not available for this customer.");
    }
  }

  public static ValidationResult ValidateAddress(CustomerAddress addressToValidate)
  {
    bool isValid;

    // Perform validation logic here and set isValid to true or false.

    if (isValid)
    {
      return ValidationResult.Success;
    }
    else
    {
      return new ValidationResult(
          "The address for this customer does not match the required criteria.");
    }
  }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.