Why are my C # Lists not working?

I'm having issues with enums in C #

I have a listing here:

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

I have an object that is trying to use an enumeration 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 will result in an error:

'int' does not contain a definition for "Dollars" and no extension method "Dollars" that accepts the first argument of type "int" can be found (do you miss the using directive or assembly references?)

I do not understand why I can not use my enum. Can someone help me explain this?

+6
source share
6 answers

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

Try to fully define its namespace:

 CostType = (int)Project.Shared.CostType.Dollars; 
+16
source

As others have pointed out, a property is โ€œcloserโ€ than a type, so when a simple name is resolved, the property always wins.

An interesting wrinkle in this situation is that if you made a property of type CostType and not int, it would work.

The reason for this is that C # has a special rule only for this situation, usually called the "Color Color" rule. (Because the situation most often arises: the type Color and the property Color of Color).

If a simple name can mean both a type and a property or field with that name, then C # allows you to access static members through type and instance members through a property. This allows you to do things like:

 Color = Color.Red; description = Color.ToString(); 

The first and third colors are property references; the second is a type reference.

+12
source

You have a name conflict. You named your CostType property, which matches the name of the enumeration. Thus, C # assumes that both CostType references in the Foo() method refer to a property, not an enumeration.

You can try the following:

 CostType = (int)Project.Shared.CostType.Dollars; 
+1
source

The problem is that in CostType = (int)CostType.Dollars; Both CostType events CostType related to a variable. And the CostType variable CostType not have a property called Dollars . Include a namespace to refer to the Project.Shared.CostType type to explicitly refer to the type, not the variable.

+1
source

Change it to this and it should work

 public enum CostType { Dollars, Percent } public class Foo { public int CostTypes { get; set; } public Foo() { CostTypes = (int)CostType.Dollars; // <--- error syntax highlighting on 'Dollars' } } 

I would not name my auto property with the same name as my enum CostType, which I tested, and what I published, compiled and worked

+1
source

I decided: "ENUM does not contain a definition for VALUE" by restarting Visual Studio. Just in case, someone has the same problem.

0
source

Source: https://habr.com/ru/post/904410/


All Articles