[Mono-list] Enum problem

gennady wexler adyler@winisp.net
Sat, 16 Oct 2004 11:52:00 -0700


I think what we wanted to know was why based on different execution sources
we get:

case 1: Negativo prints first
case 2: Neutro prints first

it appears they are aligned differently by compiler (jit in this case).

it is not a question anymore whether these two values are of the same
weight/value.

there are attributes you can specify for enum declaration, I think mono
defaults to something different than what .net does.

it doesn't hurt final product here though, program wont break in such cases=
.
it will just behave differently than what coder expects it to. :)

we are just being a bit too perfectionist here, we want print out look
exactly the same as it comes out from under .net environment.

so to write this code cleanly you would have to use recommended practices,
that is it would be something like this:

enum EnumVal {
    enFirstValue =3D 1,
    enSecondValue,
    enThirdValue =3D 20,
    enFourthValue
}

this way compilers will take care of assigning unique values to each of the
values, then your code will behave as you expect it to.

now, to make the original code in this mail thread to work, you will need t=
o
tell compiler the base type of enum construct:

enum DoubleEnumVal : double { // or whatever type you want
    enFirstVal =3D .3, // or I presume 1/3 should also work
    enSecond,
    enThird
}

however, I almost never see anyone had a need for this kind of precision,
have you?

so there really is no problem here, just matter of proper use of the
language.

does this make sense?


On 10/16/04 11:28 AM, "Kirk Marple" <kirk-public@agnostic-media.com> wrote:

> The real issue here is about rounding to integers, and enums are ints by
> default, so really the numbers are equivalent to:
>=20
> Positivo =3D 0,
> Negativo =3D 1,
> Neutro =3D 1
>=20
> Which would make sense by what you see printed out.
>=20
> -----Original Message-----
> From: mono-list-admin@lists.ximian.com
> [mailto:mono-list-admin@lists.ximian.com] On Behalf Of gennady wexler
> Sent: Saturday, October 16, 2004 10:54 AM
> To: Iain McCoy; francis@aspl.es
> Cc: mono
> Subject: Re: [Mono-list] Enum problem
>=20
> On 10/16/04 4:33 AM, "Iain McCoy" <iain@mccoy.id.au> wrote:
>=20
>> On Sat, 2004-10-16 at 13:17 +0200, Francis Brosnan Bl=E1zquez wrote:
>>> Hi.
>>>=20
>>> Working with enumerations I've found an strange behaviour. If you
>>> compile the following source code and run it:
>>>=20
>>> --
>>> using System;
>>>=20
>>> public class EnumTest {
>>>=20
>>> public enum TipoCarga {
>>> Positivo =3D 1/3,
>>> Negativo =3D 1 + (1/3),
>>> Neutro   =3D 1,
>>> }
>=20
>> 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
>=20
> if 1/3 is 0 then how would 1 + 1/3 be 0 too?
>=20