Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 923
user3407591 Avatar asked Mar 11 '14 19:03

user3407591


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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;
    }
}
like image 163
Jon Skeet Avatar answered Sep 28 '22 17:09

Jon Skeet


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;
like image 39
Vlad Avatar answered Sep 28 '22 16:09

Vlad