C# How to use get, set and use enums in a class -
i have program use class store settings. need use set , functions change , store settings. have tried this, , don't work. can me one?
private enum _difficulty { easy, normal, hard }; public void setdifficulty(difficulty) { _difficulty = difficulty; } public enum getdifficulty() { return _difficulty; } is there no way use enums in class get , set?
i need bool , int.
there several things wrong here:
- your enum private, methods public. therefore can't make methods return type enum type, or have parameters type
- your
setdifficultymethod has parameter ofdifficulty- meant parameter name or type? - your
setdifficultymethod trying set type rather field - your
getdifficultymethod trying useenumreturn type, , returning type rather field
basically, seem confused enum declaration declaring - it's not declaring field, it's declaring type (and specifying named values of type are).
i suspect want:
// try not use nested types unless there's clear benefit. public enum difficulty { easy, normal, hard } public class foo { // declares property of *type* difficulty, , *name* of difficulty public difficulty difficulty { get; set; } } you can use get/set methods if want make code java instead of c#:
public enum difficulty { easy, normal, hard } public class foo { private difficulty difficulty; public void setdifficulty(difficulty value) { difficulty = value; } public difficulty getdifficulty() { return difficulty; } }
Comments
Post a Comment