checking a bool via string
To save writing loads of code, I'd like to check if a bool is true based on the string "boolName", which can be changed on the fly.
Is there anyway of doing:
string boolName = bool1;
if (boolName == true) {
print ("true");
}
Thanks
UPDATE:
I discovered hashtables, which gave me a better way round of doing this. Thanks for the replies, helped me narrow down my options and reach a better solution
why on earth would you over complicate it like that? if it's a bool and can only be true
or false
, then use a bool
.
is there a reason why you don't want to, apart from writing more code and making it slower?
Because im doing a loop of about 20 items and i dont want to write 20 "if (bool1).. if (bool2) etc
if you've got a loop, then why not use a List
or array of bool
s?
perhaps you can show me an example of your code to better understand why you want to do it that way. like i mentioned, it seems like you're making it more difficult for yourself...
Sorry but on the face of it this just seems crazy. How could what you're asking for simplify things? $$anonymous$$aybe I'm missing something. Like gjf suggests, it would be easier to understand if you were to show us how your loop would work.
After re-read your question, I think to you want to say to the string boolName
will be true
when contains the serie of characters "true", so you can use the Contains()
method
if (boolName.Contains("true")){
print("true");
}
@DCordoba - i don't think that i misunderstood, but what the OP seems to be asking doesn't sound like a real solution to anything. if there was a better understanding of the use case, we'd be able to offer better advice.
...which is why i asked to see some code - checking strings is never going to be as simple/fast as a bool...
@gjf yes, the bool is 8-bit, less than an extended character(32-bit), takes less memory and is more easy to process, but he not was asking about how to do well or improve that, is asking about exact solution. Not misunderstood me, is good help, but when somebody ask for this help, otherwise probably not will listen, just use other politic like "here you have what you looking for, but maybe not is the better way, try this way to no overheat in some simplest as that", but if you just say "that's trash (to say it better as possible) try this and all" probably not are helping nobody, just making someone to do his way just to go against you.
in other way, I think to the question is ambiguous, maybe he have to edit it, extend to an exact example of use, more specific to "is there anyway of doing:" and a code out of context.
The way i understand the question, it's the same one asked here every week: "how do i reference a variable based on it's name as a string?". Like:
string boolName = "bool1";
// and then have some way of checking
if (valueOfBoolWithThisName(boolName) == true) {
print ("true");
}
And the answer i'd give is the same as previous weeks: yeah you can do stuff like that with reflection, but most likely you can avoid this whole problem by re-structuring your logic. And even if not, it would be better to make a Dictionary or an array of the booleans and reference the booleans from that with index (array) or $$anonymous$$ey string (dictionary)
This is actually a pretty common (deadend) idea for people who haven't seen arrays. The idea is usually to make vars named n1, n2, n3 ... and then use i an an index. You make string ("n"+i), then have to figure out how to map "n3" back to the n3 variable.
Of course, arrays are the real solution, and are much better.