-1

I have four boolean variables. I have initalised the list radioBtns with the name of each of these variables as strings.

I want to loop through each of the strings in the list and set each boolean variable (which each have the same name as the strong) to true.

I receive this error though:

Cannot assign to 'Btn' because it is a 'foreach iteration variable'

bool Q1Button1_True, Q2Button1_True, Q3Button1_True, Q4Button1_True;

List<string> radioBtns = new List<string>(new string[] { "Q1Button1_True", "Q2Button1_True", "Q3Button1_True", "Q4Button1_True" });

foreach (string Btn in radioBtns)
{
   Btn = true;
}
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • 5
    This code makes no sense. `Btn` is a *string*, not a bool. What are you trying to do? And as the error says, `Btn` is the iteration variable, not something that can be assigned to. It provides the current list item, it's not someplace where you can store your owndata – Panagiotis Kanavos Sep 30 '19 at 09:15
  • 1
    Please describe what you want to achieve? – EFrank Sep 30 '19 at 09:15
  • I want to set all of the boolean values to true – The Codesee Sep 30 '19 at 09:15
  • The code you use does nothing like that. Where did those variables come from and why don't you just set them to true in the declaration? – Panagiotis Kanavos Sep 30 '19 at 09:16
  • Explain the *actual* problem, not what you think the solution would look like. Perhaps you need a `Dictionary` instead of a list and some raw variables. Or you really need something different because you're trying to *bind* a group of radio buttons to some data – Panagiotis Kanavos Sep 30 '19 at 09:16
  • 1
    @TheCodesee you keep describing your attempted solution, not the actual problem. A simple `bool[]` array is enough. to get 4 bools that can be set to true or false. Sweeper's answer shows how easy that is – Panagiotis Kanavos Sep 30 '19 at 09:19

2 Answers2

5

Instead of four different variables, you should use an array:

bool[] QButton_True = new bool[4];
for (int i = 0 ; i < 4 ; i++) {
    QButton_True[i] = true;
}

Instead of referring to them as Q1Button_True, Q2Button_True..., you refer to them as QButton_True[0], QButton_True[1]...

Sweeper
  • 213,210
  • 22
  • 193
  • 313
-1

You could use reflection for this, but it might be overkill for this scenario.

If you are simply setting all variables to true, you might want to just set them on the source.

bool Q1Button1_True = true;
bool Q2Button1_True = true;
bool Q3Button1_True = true;
bool Q4Button1_True = true;

Also, if this is something that will grow, let's say to Qn then you might want to have an array, where instead of Q1Button1_True you would have Button1_True[1].

I am assuming you will be doing this is multiple parts of your application, so it might be a wise choice to have some "dynamic" setters. In this case, you can use reflection:

bool Q1Button1_True, Q2Button1_True, Q3Button1_True, Q4Button1_True;

List<string> radioBtns = new List<string>(new string[] { "Q1Button1_True", "Q2Button1_True", "Q3Button1_True", "Q4Button1_True" });

foreach (string Btn in radioBtns)
{
   bool buttonVar = this.GetType().getProperty(Btn).getValue(this, null);
   buttonVar = true;
}

Another way of doing it is to use something like this:

bool Q1Button1_True, Q2Button1_True, Q3Button1_True, Q4Button1_True;

List<string> radioBtns = new List<string>(new string[] { "Q1Button1_True", "Q2Button1_True", "Q3Button1_True", "Q4Button1_True" });

foreach (string Btn in radioBtns)
{
   PropertyInfo buttonVar = this.GetType().GetProperty(Btn);
   buttonVar.SetValue(this, true, null);
}

Hope this helps!

ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • 1
    On the contrary, it's not. It's a perfect case of the XY Problem. There's no reason to set variables like this when one can just *set* them during declaration. Or use a loop over a bool array – Panagiotis Kanavos Sep 30 '19 at 09:17
  • It's a perfect use-case for a simple `for` loop – FCin Sep 30 '19 at 09:18
  • @PanagiotisKanavos you are correct, however I provided him with a way that could be more useful in the future. From experience this type of request usually comes when someone is implementing an ORM, in which case reflection is the way to go. – ZektorH Sep 30 '19 at 09:21
  • 1
    @ZektorH still, how is reflection better than a simple loop? – FCin Sep 30 '19 at 09:41