So I have an XML file where the value in an element is boolean. It’s literally written in the file as “True” or “False”.
I’m loading the XML Node elements and their values into a dictionary and stepping through that dictionary’s entries (
de), so now these values are variants. In most cases, the value is an integer, and reading the value out as
de.value.IntegerValue works fine in those cases. When I try this with a boolean,
de.value.BooleanValue, I always get False regardless of the actual value.
Testing the variant with
VarType(de.value) tells me the value is a string. So how do I force it to treat this as a boolean? shouldn’t appending
.BooleanValue to the variant do this, when the current value is either True or False? Or is it looking for something else like 0 or 1 in this case?
Make sure that the string does not have a space or any other special/invisible character.
nothing weird in there:
Var myValue As Variant = someXMLthing
Var myResult As Boolean = myValue
you can convert like this can’t you ?
A variant is a converts to the type it matches first so make sure you convert correctly to the actual type when you can.
I can, and that works - thanks.
Strange though, that appending
.BooleanValue to the variant doesn’t. isn’t that the same thing?
Well it should do the same thing i guess but it’s still received from a variant so if you assign that to a variant maybe the compiler prefers string (or checks it first) and somehow accepts it?
Just report an issue with a sample project they may fix it
Can you provide a sample project that shows this problem?
This code converts a Variant containing the String “True” to a Boolean set to True:
Var vTest As Variant = "True"
Var vBool As Boolean = vTest.BooleanValue
// vBool = True
If your code is not working the same as this then perhaps the value you are getting from your Dictionary does not contain what you think it does.
What wasn’t working for me was treating the value as Boolean directly on the variant. eg:
de.Value.BooleanValue. I know the value of
de.Value is the string “False” (in the dictionary) because I can see that in the debugger. When I changed it to True in the XML file and reloaded, it showed that dictionary entry
de.Value was the string “True” but it evaluated as false.
So I set up a simple test project and now it works using
.BooleanValue. I’m not sure why, but it does. I copy/pasted the code out of my other project and changed the variable names, so it’s basically the same.
But this is all moot now, because it’s working in the original project as well. Maybe there was something I missed, but I’ve been careful to not change much as I was trying to figure this out. Either way - both methods, explicitly setting the value
As Boolean and reading the value with
.BooleanValue appended to it, are working. So i guess problem solved? or at least, no longer a problem…