Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DataAnnotations - Multiple Acceptable Values

DataAnnotations - Multiple Acceptable Values

Scheduled Pinned Locked Moved Visual Basic
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    I was looking into using data annotations to help validation on one of my classes. However, in my class there is a property called CommandFamily which is a Byte. This property must have a value of 0x01 - 0x07 or 0xFF. I have already applied the Required attribute and was looking into using the Range. However the Range attribute can only accept a minimum and maximum value and you cannot apply more then one Range attribute. I checked on MSDN and I don't see any other attribute that will allow you to apply a list of values. I would really like to avoid having to change the acceptable values for the CommandFamily property if possible. Is there any way data annotation attribute, or combination, that will allow me to specify a range of acceptable values, instead of just a single range? Thanks in advance for any assistance or guidance on this.

    A black hole is where God tried to divide by zero. There are 10 kinds of people in the world; those who understand binary and those who don't.

    Richard DeemingR 1 Reply Last reply
    0
    • D Dominick Marciano

      I was looking into using data annotations to help validation on one of my classes. However, in my class there is a property called CommandFamily which is a Byte. This property must have a value of 0x01 - 0x07 or 0xFF. I have already applied the Required attribute and was looking into using the Range. However the Range attribute can only accept a minimum and maximum value and you cannot apply more then one Range attribute. I checked on MSDN and I don't see any other attribute that will allow you to apply a list of values. I would really like to avoid having to change the acceptable values for the CommandFamily property if possible. Is there any way data annotation attribute, or combination, that will allow me to specify a range of acceptable values, instead of just a single range? Thanks in advance for any assistance or guidance on this.

      A black hole is where God tried to divide by zero. There are 10 kinds of people in the world; those who understand binary and those who don't.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The simplest option would probably be to use the CustomValidation attribute[^] to call a Public Shared method to validate the value.

      Public NotInheritable Class CommandFamilyValidator
      Public Shared Function Validate(ByVal commandFamily As Byte) As Boolean
      Return commandFamily = &H01 _
      OrElse commandFamily = &H07 _
      OrElse commandFamily = &HFF
      End Function
      End Class
      ...
      <CustomValidation(GetType(CommandFamilyValidator), "Validate")> _
      Public Property CommandFamily As Byte
      ...

      Alternatively, you could create your own validation attribute by inheriting from the ValidationAttribute class[^]. This SO answer[^] has an example.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups