Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ValidationResult Class

Contains 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)

'Declaration
Public NotInheritable Class ValidationResult

The ValidationResult type exposes the following members.

  NameDescription
Public methodValidationResult(String)Initializes a new instance of the ValidationResult class with the specified error message.
Public methodValidationResult(String, IEnumerable(Of 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

  NameDescription
Public propertyErrorMessageGets or sets the error message for the validation result.
Public propertyMemberNamesGets the collection of member names associated with the validation result.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string value that represents the current validation result. (Overrides Object.ToString.)
Top

  NameDescription
Public fieldStatic memberSuccessA value that indicates the entity member successfully validated.
Top

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.

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


Silverlight

Supported in: 5, 4, 3

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

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

Community Additions

ADD
Show:
© 2015 Microsoft