vb.net - Elegant way to limit possible values of string property -
vb.net - Elegant way to limit possible values of string property -
i'm looking cleaner way i'm nowadays limit possible values of string property. suppose have class property "direction"
public class location private _direction string public property direction() string homecoming _direction end set(byval value string) _direction = value end set end property end class
so suppose want limit possible values of direction "north," "south," "east," , "west." realize this:
public class location private validdirection new collection public sub new validdirection.add("1", "north") ... end sub private _direction string public property direction() string homecoming _direction end set(byval value string) if me.validdirection.contains(value) _direction = value else throw new exception("invalid direction.") end if end set end property end class
but if happen create lots of location
s i've created collection on , on again. illustration it's not such big deal, suppose have 25 possibilities 5 or more properties , create hundreds or thousands of location
s, starts add together up.
is there improve way accomplish this?
i should note, in case complicates things, i'd translate input accepted value in cases. example, "n" used , i'd automatically turn "north."
any ideas?
as suggested plutonix, should defining enumeration:
public enum direction north south east west end enum
you declare property type. have used number of enumerations, e.g. dialogresult
returned showdialog
, number of them used messagebox.show
. there number may have used in winforms designer, e.g. dock
, anchor
properties of forms , controls, style
property of progressbar
, dropdownstyle
of combobox
.
note values represented numbers under hood - integer
values starting @ 0 default - , result of calling tostring
give name. if need other name, e.g. need include spaces, can apply description
attribute , write simple code that.
vb.net properties
Comments
Post a Comment