Gaute Haugen
Gaute Haugen

Reputation: 153

C# How to use get, set and use enums in a class

I have a program where I use a class store settings. I need it to use set and get functions to change and store settings. I have tried this, and I don't get it to work. Can anyone help me with this one?

    private enum _Difficulty { Easy, Normal, Hard };

    public void SetDifficulty(Difficulty)
    {
        _Difficulty = Difficulty;
    }

    public enum GetDifficulty()
    {
        return _Difficulty;
    }

Is there no way to use enums in a class with get and set?

I also need this with bool and int.

Upvotes: 17

Views: 123582

Answers (7)

D Stanley
D Stanley

Reputation: 152501

An enum is a type not a variable. Something like this would work:

public enum Difficulty { Easy, Normal, Hard };

private Difficulty _Difficulty;

public void SetDifficulty(Difficulty difficulty )
{
    _Difficulty = Difficulty;
}

public Difficulty GetDifficulty()
{
    return _Difficulty;
}

or more succinctly as an Auto Property:

private Difficulty Difficulty {get; set;}

Note that the enum has to be public if you want it to be accessible from public methods.

Upvotes: 0

Vlad
Vlad

Reputation: 1919

You code tries to assign Difficulty a value, when in fact Difficulty is the name of the enum type. I would encourage use of getters and setters as properties instead:

public enum Difficulty { Easy, Normal, Hard };

private Difficulty _difficulty;

public Difficulty CurrentDifficulty
{
    get { return _difficulty; }
    set { _difficulty = value; }
}

This way you can add additional code in the setter for special conditions. To use it you simply do the following:

//set
CurrentDifficulty = Difficulty.Easy;

//get
Difficulty theDifficulty = CurrentDifficulty;

Upvotes: 7

David
David

Reputation: 218798

There are a number of issues here:

  1. Your enum is private, so nothing will be able to call SetDifficulty to provide it with a value from that enum. Indeed, the compiler won't allow this.
  2. The argument to SetDifficulty is just a type, it also needs a variable name. Also a compiler error.
  3. You're trying to get/set the value of the enumeration itself, rather than a class-level variable of the type of the enumeration. This too won't compile.

It looks like you want to do this:

public enum Difficulty { Easy, Normal, Hard }

public Difficulty DifficultySetting { get; set; }

Note that I had to change the name of the property to DifficultySetting because it conflicts with the enum name. Yours used an underscore, which would also solve that problem. However I always found that underscores are for private members and if you want consuming code to use this enum then it would need to be public. Decorating a public member with things like underscores distracts from the semantics of the name itself. In any event, the semantics are up to your personal preference, so you can modify these names as you see fit.

Upvotes: 1

DLeh
DLeh

Reputation: 24385

Once you specify an enum using the enum keyword, that enum acts as a type, like a class or struct would.

Here's how you'd implement a property with a custom enum:

public enum _Difficulty { Easy, Normal, Hard };
public _Difficulty Difficulty { get; set; }

Upvotes: 5

cost
cost

Reputation: 4480

The enum you want to use needs to be public. You also need an instance of that enum to store the value you're setting, you're currently setting it to your enum's declaration. Then you want to use the same enum type for what your class stores and what you pass in.

public enum Difficulty { Easy, Normal, Hard };

public Difficulty { get; set; }

Upvotes: 1

ChengDuum
ChengDuum

Reputation: 77

I'm not sure you are using them correctly. This might help...

private enum Difficulty { Easy, Normal, Hard };
private Diffuculty theDifficulty;

public void SetDifficulty(difficulty d)
{
    theDifficulty = difficulty;
}

public Difficulty GetDifficulty()
{
    return theDifficulty;
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

There are several things wrong here:

  • Your enum is private, but your methods are public. Therefore you can't make your methods return type be the enum type, or have parameters with that type
  • Your SetDifficulty method has a parameter of just Difficulty - is that meant to be the parameter name or the type?
  • Your SetDifficulty method is trying to set the type rather than a field
  • Your GetDifficulty method is trying to use enum as a return type, and is then returning a type rather than a field

Basically, you seem to be confused about what your enum declaration is declaring - it's not declaring a field, it's declaring a type (and specifying what the named values of that type are).

I suspect you want:

// Try not to use nested types unless there's a clear benefit.
public enum Difficulty { Easy, Normal, Hard }

public class Foo
{
    // Declares a property of *type* Difficulty, and with a *name* of Difficulty
    public Difficulty Difficulty { get; set; }
}

You can use get/set methods if you really want to make your code look like 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;
    }
}

Upvotes: 50

Related Questions