dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[DotGNU]Coercion - 0 to enum


From: Jonathan P Springer
Subject: [DotGNU]Coercion - 0 to enum
Date: Thu, 15 Aug 2002 16:12:37 -0400
User-agent: Mutt/1.4i

I ran across this fooling around with pnetlib.

This code:

<SNIP>
using System;

[Flags]
public enum AttrEnum {
        attr0 = 0x0001
}

public class ConcreteAttrClass {
        private AttrEnum a;
        public AttrEnum Attr { get { return a; } }

        public ConcreteAttrClass() { a = 0; }

        public void SetAttr0() { a = AttrEnum.attr0; }

        public static void Main() {
                ConcreteAttrClass x = new ConcreteAttrClass();
                if (x.Attr == 0) {
                        Console.WriteLine("Starts out as 0.");
                } else {
                        Console.WriteLine("Starts out non-zero(?)");
                }
                x.SetAttr0();
                if ((x.Attr & AttrEnum.attr0) != 0) {
                        Console.WriteLine("Attribute set successfully.");
                } else {
                        Console.WriteLine("Attribute not set successfully.");
                }
        }
}
</SNIP>

Gives the following compile errors:
concreteenum.cs:18: invalid operands to binary `=='
concreteenum.cs:18: no conversion from `int' to `bool'
concreteenum.cs:24: invalid operands to binary `!='
concreteenum.cs:24: no conversion from `int' to `bool'

My "C# in a Nutshell" says that literal "0" should be coerced to any
enum.  (In fact, explicit casting removed the errors.)  I even see the
code in cg_coerce.c.  So, am I inept, is my book, or is this a compiler
bug/not yet implemented?

Thanks,
-js

-- 
-Jonathan P Springer <address@hidden>
------------------------------------------------------------------------------
"A standard is an arbitrary solution to a recurring problem." - Joe Hazen


reply via email to

[Prev in Thread] Current Thread [Next in Thread]