Results 1 to 10 of 10

Thread: ENUM Restrictions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    ENUM Restrictions

    Is there a way when using and enumerated field in a class, to restrict the values of the property to only the ones enumerated? For example...

    Code:
    Enum AttendanceNotes
        Present = 0
        Tardy = 1
        Absent = 2
        SchoolTrip = 4
        ResourceLab = 8
    End Enum
    What if I try to enter 5, which is not one of the choices? Will VB allow me to enter 5 or will it say 5 is not a valid value for the property? I want to disallow values that are not listed.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ENUM Restrictions

    if the property type is the enum type.... I *think* it'll restrict it... not at design time, but at run time...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: ENUM Restrictions

    it actually will restrict it at design time so long as you code with option strict on.

    Trying to assign 5 would result in a type mismatch because with option strict on, the compiler won't make any attempt to implicitly convert an integer value 5 to an enum value, even though the enums are technically just integers as well.

    If you code with option strict off, the compiler tries to make the conversion, and will assign the integer to the enum property, even if its not a valid enum.

    This is all the more reason to code with option strict ON! it forces better code and leaves less errors to be discovered later on.

    Obviously coding with option strict is the correct and easy solution to restrict this. If for some reason, that is not an option for you, then you will need to write code in your set statement of the property, to validate the enum before you set the value.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: ENUM Restrictions

    I think his question is, at runtime, if he has a textbox for the user to enter in an enum value, how can he limit the user from typing in only the valid enum value?
    If my interpretation of his question is correct then the answer is no, you can't. However, instead of using a textbox, you can use a combobox or listbox or domainupdown control to force the user to pick only the available choices.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: ENUM Restrictions

    well I guess if you are right stanav, then my next question would be why force the user to enter a number at all for such things like "tardy" and "SchoolTrip"

    I would have the user selecting string values like "School Trip" and then convert those to the enum values behind the scenes. Why should the end user need to remember integer values that represent real world things?

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: ENUM Restrictions

    Quote Originally Posted by kleinma
    well I guess if you are right stanav, then my next question would be why force the user to enter a number at all for such things like "tardy" and "SchoolTrip"

    I would have the user selecting string values like "School Trip" and then convert those to the enum values behind the scenes. Why should the end user need to remember integer values that represent real world things?
    Yes, that's what I would do too... And I certainly wouldn't use a textbox for it

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: ENUM Restrictions

    no.. if the choices are that finite, then the selection method should be restrictive... combobox (or listbox if room is available) are the best options.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: ENUM Restrictions

    Option Strict will force you to pass in an object of the Enum type but it wont check that the value is actually defined in the Enum until runtime.

    With Option Strict On this fails:
    vb Code:
    1. Me.TestDesignTime("Tired")
    With Option Strict On this passes:
    vb Code:
    1. Me.TestDesignTime(CType([Enum].Parse(GetType(AttendanceNotes), "Tired"), AttendanceNotes))
    The method being:
    vb Code:
    1. Public Sub TestDesignTime(ByVal value As AttendanceNotes)
    Yet both will fail at runtime.

    The Enum class has an IsDefined shared method to test a value to see if it is in the range of a given Enum type. This allows you to test the input before converting it to the Enum type without relying on slow error handling.
    vb Code:
    1. Public Enum AttendanceNotes
    2.    Present = 0
    3.    Tardy = 1
    4.    Absent = 2
    5.    SchoolTrip = 4
    6.    ResourceLab = 8
    7. End Enum
    8.  
    9. Public Sub EnumCheckExample()
    10.  
    11.    MsgBox([Enum].IsDefined(GetType(AttendanceNotes), "Tardy").ToString) 'true
    12.    MsgBox([Enum].IsDefined(GetType(AttendanceNotes), 8).ToString) 'true
    13.  
    14.    MsgBox([Enum].IsDefined(GetType(AttendanceNotes), "Sick").ToString) 'false
    15.    MsgBox([Enum].IsDefined(GetType(AttendanceNotes), 3).ToString) 'false
    16.  
    17. End Sub
    Last edited by Edneeis; Mar 21st, 2008 at 01:19 AM.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: ENUM Restrictions

    That is assuming you are parsing a string into an enum... He made it sound as though he was using numbers. There is a good chance he is coding with option strict off, which is basically open to all these types of implicit conversion issues.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: ENUM Restrictions

    Dang! Look at this can of worms. I have Strict On. My Internet connection at home is down so I have to come to my classroom at school to post here until I'm reconnected at home. I was asking some other questions and figured I'd put in a few others while I was here. But I'm unable to view my code while I'm here at school. Anyway, this was not a big issue for me. It just occurred to me that there might be a way to do the restriction.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width