Question store Radio button result?

Harshitha Gowda

New member
Joined
Jun 13, 2021
Messages
1
Programming Experience
Beginner
I am building an application which recommends course to the student and for that i have developed 3 screen , screen 1 had certain subjects and and each subject has radio button with label yes and no likewise i have two other screens which has set of hobbies and moral values . How do i store the result of them and recommend them a course based on thier yes/No answer when they click radio button ?
Regards
 
RadioButtons are used to allow one selection from multiple mutually-exclusive options. If you need to store such a selection, the best way is probably to use an enum. For instance, lets say that you were providing the means to build a fantasy character and the user had to select a fundamental element to associate their character with. You might define an enum like so:
C#:
public enum FundamentalElement
{
    Earth,
    Air,
    Fire,
    Water
}
You might then have four RadioButtons labelled with the names of the four elements. You might haver a class that stores the user's selections and it might have a property for that selection like so:
C#:
public class CharacterTraits
{
    // ...
    public FundamentalElement FundamentalElement { get; set; }
    // ...
}
You could then store the selection like so:
C#:
var traits = new CharacterTraits();

if (earthRadioButton.Checked)
    traits.FundamentalElement = FundamentalElement.Earth;
else if (airRadioButton.Checked)
    traits.FundamentalElement = FundamentalElement.Air;
else if (fireRadioButton.Checked)
    traits.FundamentalElement = FundamentalElement.Fire;
else if (waterRadioButton.Checked)
    traits.FundamentalElement = FundamentalElement.Water;
Alternatively, you could start out by assigning the FundamentalElement values to the Tag properties of the corresponding RadioButtons and then do this:
C#:
var traits = new CharacterTraits();

traits.FundamentalElement = (FundamentalElement) Controls.OfType<RadioButton>()
                                                         .Single(rb => rb.Checked)
                                                         .Tag;
You can then do whatever you want with that CharacterTraits object and its FundamentalElement property. The same principle applies to your situation. Your enum will have a different name and different fields and your class to store the value will be different, but it will all work the same way.
 
As a quick aside, when a control is supposed to hold a yes/no answer, the correct UI control to use is a check box, not a radio button. Yes, I know that we are used to paper forms/questionnaires where there is a column for yes, and another column for no, and that it maybe confusing for some people to switch over mentally to checking a checkbox for yes, and not checking it for no. And yes, there is also the additional issue of detecting whether the user truly meant to say no, or they just simply forgot to respond to the question.
 
I missed the bit where the RadioButtons were representing only "yes" and "no". In that case, my solution is over-engineered and, as suggested, you should use a CheckBox and assign the value of its Checked property to a bool property somewhere. You could sort of think of the bool type as an enum with true and false being its fields.
 
Back
Top Bottom