|
-
Mar 20th, 2008, 01:01 PM
#1
Thread Starter
Hyperactive Member
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.
-
Mar 20th, 2008, 01:07 PM
#2
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
-
Mar 20th, 2008, 01:47 PM
#3
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.
-
Mar 20th, 2008, 03:32 PM
#4
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.
-
Mar 20th, 2008, 03:43 PM
#5
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?
-
Mar 20th, 2008, 03:49 PM
#6
Re: ENUM Restrictions
 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
-
Mar 20th, 2008, 03:50 PM
#7
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.
-
Mar 21st, 2008, 12:58 AM
#8
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:
Me.TestDesignTime("Tired")
With Option Strict On this passes:
vb Code:
Me.TestDesignTime(CType([Enum].Parse(GetType(AttendanceNotes), "Tired"), AttendanceNotes))
The method being:
vb Code:
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:
Public Enum AttendanceNotes
Present = 0
Tardy = 1
Absent = 2
SchoolTrip = 4
ResourceLab = 8
End Enum
Public Sub EnumCheckExample()
MsgBox([Enum].IsDefined(GetType(AttendanceNotes), "Tardy").ToString) 'true
MsgBox([Enum].IsDefined(GetType(AttendanceNotes), 8).ToString) 'true
MsgBox([Enum].IsDefined(GetType(AttendanceNotes), "Sick").ToString) 'false
MsgBox([Enum].IsDefined(GetType(AttendanceNotes), 3).ToString) 'false
End Sub
Last edited by Edneeis; Mar 21st, 2008 at 01:19 AM.
-
Mar 21st, 2008, 08:35 AM
#9
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.
-
Mar 21st, 2008, 01:01 PM
#10
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|