-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?

6
  • What do you mean "convert string and int to bool name"? What are you trying to accomplish? Commented Jul 10, 2015 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 Commented Jul 10, 2015 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. Commented Jul 10, 2015 at 18:15
  • see stackoverflow.com/questions/2566101/… Commented Jul 10, 2015 at 18:16
  • the only method that I can see then is @sstan, why are you not able to use an array? Commented Jul 10, 2015 at 18:17

1 Answer 1

7

Use an array instead:

bool[] abc;

// ...

if (abc[counter] == true) {
{
    // some code.
}
Sign up to request clarification or add additional context in comments.

6 Comments

can't use array in me case. it is possible to convert string to bool name?
@user5081068 You can't take a value and just magically get it's variable name. Why can't you use arrays?
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.
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.
ok i will try to use array. thanks for help :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.