-4

I trying to make something like that:

    // int counter; - this is changing in ther other parts of program
    bool abc1; bool abc2; bool abc3; bool abc4;

    if("abc" + counter == true)
    {
     //some code
    }

Anyway, I need to convert string and int to bool name. How can I do this?

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • What do you mean "convert string and int to bool name"? What are you trying to accomplish? – PC Luddite Jul 10 '15 at 18:13
  • your question is unclear, please describe in detail what you are trying to accomplish. ("abc" + counter == true) will never evaluate to true and I am having a hard time understanding what you are attempting to accomplish – mgrenier Jul 10 '15 at 18:14
  • He's trying to form the name of a variable from a string and an int in order to reference that variable. Possibly doable via reflection, but much better to use @sstan's approach. – Eric J. Jul 10 '15 at 18:15
  • see http://stackoverflow.com/questions/2566101/how-to-get-variable-name-using-reflection – Ryan Langton Jul 10 '15 at 18:16
  • the only method that I can see then is @sstan, why are you not able to use an array? – mgrenier Jul 10 '15 at 18:17
  • Are those bools **local** to a method?...or are they class level variables? – Idle_Mind Jul 10 '15 at 19:06

1 Answers1

7

Use an array instead:

bool[] abc;

// ...

if (abc[counter] == true) {
{
    // some code.
}
sstan
  • 35,425
  • 6
  • 48
  • 66
  • can't use array in me case. it is possible to convert string to bool name? – user5081068 Jul 10 '15 at 18:14
  • 3
    @user5081068 You can't take a value and just magically get it's variable name. Why can't you use arrays? – gunr2171 Jul 10 '15 at 18:15
  • 3
    What you are trying to do doesn't sound like a good idea, as it sounds like you want to do some reflection. If you are going to allow yourself to go down that path, then I have a hard time believing that you can't use an array. – sstan Jul 10 '15 at 18:16
  • If you really cannot use arrays, the next best alternative is a long chain of `if` statements `if (abc1) { /* Some code /* } if (abc2) { /* Some code /* }` etc. Even if you can use reflection (depends on where abcN are defined), that is a bad path to walk down. – Eric J. Jul 10 '15 at 18:18
  • ok i will try to use array. thanks for help :) – user5081068 Jul 10 '15 at 18:19
  • SO peer pressure FTW :) – sstan Jul 10 '15 at 18:19