-1

I have a form, and I use check boxes to assign a string when clicked. However, I noticed I have the error: "The field is assigned but its value is never used". I ignored it at first but when I run the code, it returns nothing, even though a string is assigned later on.

public partial class FindQuestionMenu : Form
{
    private string selectedMode;
    private string selectedTopic;
    public FindQuestionMenu()
    {
        InitializeComponent();
    }
    private void PPQModeSelect_CheckedChanged(object sender, EventArgs e)
    {
        selectedMode = "Past Paper Questions Mode";
    }
    private void RandomQuestionSelected_CheckedChanged(object sender, EventArgs e)
    {
        selectedMode = "Random Question Generator Mode";
    }
    private void SineCosineRuleOption_CheckedChanged(object sender, EventArgs e)
    {
        selectedTopic = "Sine and Cosine Rule";
        Close();
    }
    public string SelectedMode { get; set; }
    public string SelectedTopic { get; set; }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 2
    They aren't tied to your properties. Get rid of `private string selectedMode;` and just use the `SelectedMode` property. – LarsTech Aug 28 '22 at 19:54
  • 1
    Note that your code wont DEselect an option. You'll get a `CheckChanged` event when it the state goes from checked to unchecked as well. Also an `enum` or bool would be far superior to a string variable to store a finite choice – Ňɏssa Pøngjǣrdenlarp Aug 28 '22 at 19:58
  • 1
    *"The field is assigned but its value is never used"* - Well, the code shown assigns values to two fields, but never uses those fields for anything. (And has two properties which are also never referenced.) *"I ignored it at first"* - That's pretty much never a good idea. *"it returns nothing, even though a string is assigned later on"* - What specifically "returns nothing" and when/how does it "return" that? What exact debugging are you doing and what exact observation are you making? If you're reading a field or property which "is assigned later on" then certainly it won't be assigned yet. – David Aug 28 '22 at 20:01
  • @David In another class I have the code `FindQuestionMenu GatherSessionInfo = new FindQuestionMenu();` and then I use `GatherSessionInfo.SelectedMode` in order to get the string assigned in this class. – Somebody Aug 28 '22 at 20:06
  • 1
    FYI: Does it really makes sense for `PPQModeSelect` and `RandomQuestionSelected` to be CheckBoxes? They cannot both be checked at the same time, because both are associated with the same property SelectedMode (which, with the program logic you are using, can have only one value at any given time, thus is being able to represent only one of `PPQModeSelect` or `RandomQuestionSelected` being checked, but not both being checked). This is a behavior commonly realized by RadioButtons, and i suggest to use RadioButtons for `PPQModeSelect` or `RandomQuestionSelected` if you have not done so already... –  Aug 28 '22 at 20:06
  • @Somebody: *"and then I use GatherSessionInfo.SelectedMode in order to get the string assigned in this class"* - Nothing in the code shown ever assigns a value to that property. Maybe this is just a typo? You have fields and properties which have names that differ only by case, maybe you just forgot which is which and don't need both? The code shown has two *properties* which are *never used* and two *fields* which are assigned but *never read*. – David Aug 28 '22 at 20:08
  • @David But the whole point is that the fields are private and then the public get set methods allow me to use the fields in a different class. Also doesn't `selectedMode = "Past Paper Questions Mode";` assign the value? – Somebody Aug 28 '22 at 20:11
  • @Somebody: If those fields are meant to hold the values for those properties then you'd need to update the properties to use them. Currently nothing connects those fields with those properties. Either use manual properties or auto-implemented properties. Currently you're mixing up the two. – David Aug 28 '22 at 20:13
  • 1
    See [What are Automatic Properties in C# and what is their purpose?](https://stackoverflow.com/q/6001917/719186) – LarsTech Aug 28 '22 at 20:14
  • @Somebody Variable names/ Class names/ Methods/ Fields/ etc. are all **case-sensitive**. `selectedMode` does not equal `SelectedMode`. This is essentially a typo. – Ibrennan208 Aug 28 '22 at 20:24

1 Answers1

1

Please note, that field

private string selectedMode

has nothing common with property

public string SelectedMode { get; set; }

It seems, that you don't need explicit backing fields at all and you can assign values to properties:

public partial class FindQuestionMenu : Form
{
    public FindQuestionMenu()
    {
        InitializeComponent();
    }

    private void PPQModeSelect_CheckedChanged(object sender, EventArgs e)
    {
        SelectedMode = "Past Paper Questions Mode";
    }

    private void RandomQuestionSelected_CheckedChanged(object sender, EventArgs e)
    {
        SelectedMode = "Random Question Generator Mode";
    }

    private void SineCosineRuleOption_CheckedChanged(object sender, EventArgs e)
    {
        SelectedTopic = "Sine and Cosine Rule";
        Close();
    }

    // You may want to declare "private set" not just "set"
    public string SelectedMode { get; set; }
    // You may want to declare "private set" not just "set"
    public string SelectedTopic { get; set; }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215