Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my C# enums not working?

Tags:

c#

enums

casting

I am having trouble with enums in C#

I have an enum here:

namespace Project.Shared
{
    public enum CostType
    {
        Dollars,
        Percent
    }
}

I have an object that is trying to use the enum here:

using Project.Shared;
namespace Project.Shared.FooNamespace
{
    public class Foo
    {
        public int CostType { get; set; }

        public Foo()
        {

            CostType = (int)CostType.Dollars; // <--- error syntax highlighting on 'Dollars'
        }
    }
}

This results in an error:

'int' does not contain a definition for 'Dollars' and no extension method 'Dollars' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

I do not understand why I can't use my enum there. Can someone help explain it to me?

like image 303
quakkels Avatar asked Dec 23 '11 20:12

quakkels


People also ask

Why is my Windows C drive so full?

Windows to save the deleted files in the Recycle Bin for a period (30 days or even longer by default). If you never cleared the recycle bin, it will take up a certain space on your system C drive, which as a result, your C drive is getting full. So we suggest you empty the recycle bin regularly.

Why is my C drive full and D drive empty?

If you're using a Windows machine, the most common issue is when the C drive is full and the D drive is empty. Unreasonable partition size allocation and installing too many programs can be the reasons your C drive is full, and the D drive is empty.

What do I do when my C drive is almost full?

Right-click on C: drive and select Properties, and then click "Disk Cleanup" button in the disk properties window. Step 2. In Disk Cleanup window, select the files you want to delete and click OK. If this does not free up much space, you can click Clean up system files button to delete system files.

How do I clear my local disk C space?

Open Disk Cleanup by clicking the Start button . In the search box, type Disk Cleanup, and then, in the list of results, select Disk Cleanup. If prompted, select the drive that you want to clean up, and then select OK. In the Disk Cleanup dialog box in the Description section, select Clean up system files.


1 Answers

Because your class Foo has this definition - public int CostType { get; set; } - which has more local scope than your enum.

Try fully qualifying it with its namespace:

CostType = (int)Project.Shared.CostType.Dollars;
like image 92
Yuck Avatar answered Sep 20 '22 07:09

Yuck