Results 1 to 5 of 5

Thread: Enumeration Issue...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5

    Enumeration Issue...

    I've come across the following issue with enumerations:

    Code:
    Public Enum Figures
            test1 = 1          
            test2 = 2      
    End Enum          
    
    Dim fig As Figures          
    
    'this works fine (as it should)
    fig = Figures.test1
    
    'but this also works even though its not a valid enumeration value!
    fig = 4
    Can anyone explain why this is allowed to happen and if there is a way to prevent it?

    Thanks

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Yup. You should define fig as a property and in the set you can make sure the integer that is being passed in is in the enumeration by:
    Code:
    If [Enum].IsDefined(....) Then ....

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Aren't enums are constant values so you can't change them !!! You probably should be looking at Structures not Enums .

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5
    Thanks for the suggestions guys, it looks like I'm gonna have to use a property instead. It still seems very backward though, as my idea of an enumeration is to ensure a finite list of strongly-typed values, but whats the point if you can still get away with putting any old rubbish in?

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    If you turn Option Strict on it will complain.

    Sometimes you need to be able to have a value different from the options specified. If could be a flagged value.

    A=Figures.Test1 or Figures.Test2

    In your case, that would result in A=3, which is not in the list by itself, but by a combination of Test1 and Test2.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

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