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?
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With