Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
misc
exam.security
microsoft_update_catalog
msdn.annotations
msdn.drgui.discussion
msdn.duwamish
msdn.general
msdn.magazine
msdn.soaptoolkit
msdn.webservices
msdntraining
opsmgr.connectors
opsmgr.sp1
technet
technet.howtofeedback
technet.howtoneeds
technet.magazine
technet.technettalks
  
 
date: Fri, 13 Jun 2008 01:23:00 -0700,    group: microsoft.public.msdn.general        back       


Explicit enum cast   
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?
date: Fri, 13 Jun 2008 01:23:00 -0700   author:   Vladimir Knezevic

Re: Explicit enum cast   
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
date: Fri, 13 Jun 2008 09:42:24 +0100   author:   David Hearn am

Re: Explicit enum cast   
"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!
date: Fri, 13 Jun 2008 02:33:01 -0700   author:   Vladimir Knezevic

Re: Explicit enum cast   
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!
date: Fri, 13 Jun 2008 08:27:32 -0700   author:   David Craig

RE: Explicit enum cast   
"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?
date: Sun, 20 Jul 2008 05:46:00 -0700   author:   sgmaxmp11

Re: Explicit enum cast   
"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
>
date: Sun, 20 Jul 2008 05:46:01 -0700   author:   sgmaxmp11

Re: Explicit enum cast   
"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!
date: Sun, 20 Jul 2008 05:46:01 -0700   author:   sgmaxmp11

Re: Explicit enum cast   
"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! 
> 
> 
>
date: Sun, 20 Jul 2008 05:46:02 -0700   author:   sgmaxmp11

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us