Hi! I encountered a problem, which might be something I didn't understand quite well. I'm using .NET 2.0. Suppose you have an enum defined as: public enum FooType : byte { Foo = 0, Bar = 1 } and you have the following code: FooType foot = (FooType) 2; What I expected here was InvalidCastException (or any other Exception) to be thrown, but what really happened is that foot was assigned value 2. If you check if submitted value is defined for particular enum using Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be able to assign it to FooType variable. Am I missing something? Shouldn't enum limit values used for it?
Vladimir Knezevic wrote: > Hi! > > I encountered a problem, which might be something I didn't understand quite > well. I'm using .NET 2.0. > > Suppose you have an enum defined as: > > public enum FooType : byte > { > Foo = 0, > Bar = 1 > } > > and you have the following code: > > FooType foot = (FooType) 2; > > What I expected here was InvalidCastException (or any other Exception) to be > thrown, but what really happened is that foot was assigned value 2. > > If you check if submitted value is defined for particular enum using > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be > able to assign it to FooType variable. > > Am I missing something? Shouldn't enum limit values used for it? No. enums do not restrict the ability to set values outside of the defined range. The only restriction is on the underlying type. In essence an enum is just a way of using a value type but with helpful naming to enhance readability. D
"David Hearn" wrote: > Vladimir Knezevic wrote: > > Hi! > > > > I encountered a problem, which might be something I didn't understand quite > > well. I'm using .NET 2.0. > > > > Suppose you have an enum defined as: > > > > public enum FooType : byte > > { > > Foo = 0, > > Bar = 1 > > } > > > > and you have the following code: > > > > FooType foot = (FooType) 2; > > > > What I expected here was InvalidCastException (or any other Exception) to be > > thrown, but what really happened is that foot was assigned value 2. > > > > If you check if submitted value is defined for particular enum using > > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be > > able to assign it to FooType variable. > > > > Am I missing something? Shouldn't enum limit values used for it? > > No. enums do not restrict the ability to set values outside of the > defined range. The only restriction is on the underlying type. In > essence an enum is just a way of using a value type but with helpful > naming to enhance readability. > > D > Unfortunately, this is not what I hoped for. Thanks!
Maybe you could override the assignment operator for that data variable by making it a class containing one data item. "Vladimir Knezevic" wrote in message news:AD04E133-BD78-4794-862D-EBAB18D6CB2A@microsoft.com... > > > "David Hearn" wrote: > >> Vladimir Knezevic wrote: >> > Hi! >> > >> > I encountered a problem, which might be something I didn't understand >> > quite >> > well. I'm using .NET 2.0. >> > >> > Suppose you have an enum defined as: >> > >> > public enum FooType : byte >> > { >> > Foo = 0, >> > Bar = 1 >> > } >> > >> > and you have the following code: >> > >> > FooType foot = (FooType) 2; >> > >> > What I expected here was InvalidCastException (or any other Exception) >> > to be >> > thrown, but what really happened is that foot was assigned value 2. >> > >> > If you check if submitted value is defined for particular enum using >> > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but >> > you'll be >> > able to assign it to FooType variable. >> > >> > Am I missing something? Shouldn't enum limit values used for it? >> >> No. enums do not restrict the ability to set values outside of the >> defined range. The only restriction is on the underlying type. In >> essence an enum is just a way of using a value type but with helpful >> naming to enhance readability. >> >> D >> > > Unfortunately, this is not what I hoped for. Thanks!
"Vladimir Knezevic" wrote: > Hi! > > I encountered a problem, which might be something I didn't understand quite > well. I'm using .NET 2.0. > > Suppose you have an enum defined as: > > public enum FooType : byte > { > Foo = 0, > Bar = 1 > } > > and you have the following code: > > FooType foot = (FooType) 2; > > What I expected here was InvalidCastException (or any other Exception) to be > thrown, but what really happened is that foot was assigned value 2. > > If you check if submitted value is defined for particular enum using > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be > able to assign it to FooType variable. > > Am I missing something? Shouldn't enum limit values used for it?
"David Hearn" wrote: > Vladimir Knezevic wrote: > > Hi! > > > > I encountered a problem, which might be something I didn't understand quite > > well. I'm using .NET 2.0. > > > > Suppose you have an enum defined as: > > > > public enum FooType : byte > > { > > Foo = 0, > > Bar = 1 > > } > > > > and you have the following code: > > > > FooType foot = (FooType) 2; > > > > What I expected here was InvalidCastException (or any other Exception) to be > > thrown, but what really happened is that foot was assigned value 2. > > > > If you check if submitted value is defined for particular enum using > > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be > > able to assign it to FooType variable. > > > > Am I missing something? Shouldn't enum limit values used for it? > > No. enums do not restrict the ability to set values outside of the > defined range. The only restriction is on the underlying type. In > essence an enum is just a way of using a value type but with helpful > naming to enhance readability. > > D >
"Vladimir Knezevic" wrote: > > > "David Hearn" wrote: > > > Vladimir Knezevic wrote: > > > Hi! > > > > > > I encountered a problem, which might be something I didn't understand quite > > > well. I'm using .NET 2.0. > > > > > > Suppose you have an enum defined as: > > > > > > public enum FooType : byte > > > { > > > Foo = 0, > > > Bar = 1 > > > } > > > > > > and you have the following code: > > > > > > FooType foot = (FooType) 2; > > > > > > What I expected here was InvalidCastException (or any other Exception) to be > > > thrown, but what really happened is that foot was assigned value 2. > > > > > > If you check if submitted value is defined for particular enum using > > > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but you'll be > > > able to assign it to FooType variable. > > > > > > Am I missing something? Shouldn't enum limit values used for it? > > > > No. enums do not restrict the ability to set values outside of the > > defined range. The only restriction is on the underlying type. In > > essence an enum is just a way of using a value type but with helpful > > naming to enhance readability. > > > > D > > > > Unfortunately, this is not what I hoped for. Thanks!
"David Craig" wrote: > Maybe you could override the assignment operator for that data variable by > making it a class containing one data item. > > "Vladimir Knezevic" wrote in > message news:AD04E133-BD78-4794-862D-EBAB18D6CB2A@microsoft.com... > > > > > > "David Hearn" wrote: > > > >> Vladimir Knezevic wrote: > >> > Hi! > >> > > >> > I encountered a problem, which might be something I didn't understand > >> > quite > >> > well. I'm using .NET 2.0. > >> > > >> > Suppose you have an enum defined as: > >> > > >> > public enum FooType : byte > >> > { > >> > Foo = 0, > >> > Bar = 1 > >> > } > >> > > >> > and you have the following code: > >> > > >> > FooType foot = (FooType) 2; > >> > > >> > What I expected here was InvalidCastException (or any other Exception) > >> > to be > >> > thrown, but what really happened is that foot was assigned value 2. > >> > > >> > If you check if submitted value is defined for particular enum using > >> > Enum.IsDefined(typeof(FooType), (byte)2), you'll get 'false', but > >> > you'll be > >> > able to assign it to FooType variable. > >> > > >> > Am I missing something? Shouldn't enum limit values used for it? > >> > >> No. enums do not restrict the ability to set values outside of the > >> defined range. The only restriction is on the underlying type. In > >> essence an enum is just a way of using a value type but with helpful > >> naming to enhance readability. > >> > >> D > >> > > > > Unfortunately, this is not what I hoped for. Thanks! > > >