[Mono-list] Enum problem

Harsh Busa Harsh Busa <harsh.busa@gmail.com>
Sat, 16 Oct 2004 17:43:32 +0530


op on my windows m/c installed .net sdk also mono 1.0.1

C:\Documents and Settings\HBUSA\My Documents>mcs  enum.cs
Compilation succeeded

C:\Documents and Settings\HBUSA\My Documents>enum.exe
carga value is: Positivo
carga value is: Negativo
carga value is: Negativo

C:\Documents and Settings\HBUSA\My Documents>mono enum.exe
carga value is: Positivo
carga value is: Neutro
carga value is: Neutro

Regards
Harsh


On Sat, 16 Oct 2004 21:33:57 +1000, Iain McCoy <iain@mccoy.id.au> wrote:
> On Sat, 2004-10-16 at 13:17 +0200, Francis Brosnan Bl=E1zquez wrote:
> > Hi.
> >
> > Working with enumerations I've found an strange behaviour. If you
> > compile the following source code and run it:
> >
> > --
> > using System;
> >
> > public class EnumTest {
> >
> >       public enum TipoCarga {
> >               Positivo =3D 1/3,
> >               Negativo =3D 1 + (1/3),
> >               Neutro   =3D 1,
> >       }
> >
> >       public static void Main () {
> >               TipoCarga carga;
> >
> >               carga =3D TipoCarga.Positivo;
> >               Console.WriteLine ("carga value is: {0}", carga);
> >
> >               carga =3D TipoCarga.Neutro;
> >               Console.WriteLine ("carga value is: {0}", carga);
> >
> >               carga =3D TipoCarga.Negativo;
> >               Console.WriteLine ("carga value is: {0}", carga);
> >       }
> > }
> > --
> >
> >
> > You get the following output:
> > carga value is: Positivo
> > carga value is: Neutro
> > carga value is: Neutro
> >
> > That is, on every assignment which uses TipoCarga.Negativo doesn't work=
.
> > I've been reading about how enumerations works and seens to be that you
> > can only use values from byte, sbyte, short, ushort, int, uint, long, o=
r
> > ulong types but not float or double types which can hold values as 1/3
> > and 1 + (1/3).
> >
> > The odd thing is that assignment for Positivo =3D 1/3 works perfectly.
> >
> > Maybe mcs mustn't allow someone to compile the previous source code?
>=20
> I haven't got a windows machine to run csc on to double check, but I
> suspect what you're seeing is the compiler automatically coercing all of
> your enum values to ints. This means that 1/3 =3D 0, so 1 + 1/3 =3D 0 and
> therefore TipoCarga.Neutro =3D=3D TipoCarga.Negativo. Whether this is wha=
t
> css does or not I have no idea, but according to the spec it seems to be
> correct (section 14.3 of the C# language specification 1.2):
> "If the declaration of the enum member has a constant-expression
> initializer, the value of that constant expression, implicitly converted
> to the underlying type of the enum, is the associated value of the enum
> member"
>=20
> --
> Iain McCoy <iain@mccoy.id.au>
>=20
>=20
>=20
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>=20


--=20
__________________________
http://www.ebackend.com/blo