How to convert a string to Boolean

Hello,

I can convert a string to Boolean like this…

If MyArray(125) = “True” Then
TextfieldX. Visible = True
else
TextfieldX. Visible = False
end if

but suppose I have several of those, is there a simpler way to do it rather than individually?

Thanks.

Lennox

In a module, create a method called BoolVal, with the parameter “extends s as string” and a return value of boolean. In the code:

  if s.Lowercase = "true" then
    return true
  else
    return false
  end if

You might want to also check for “yes” and other possibilities that you want to make true. To call it, you’d just say:

TextfieldX.Visible = MyArray(125).BoolVal

Bill, you don’T need the lowercase as string comparison in Xojo is case insensitive.

TextfieldX. Visible = ( MyArray( 125 ) = "true" )

The parenthesis are there for readability, but are not otherwise required.

I assume there is a good reason that MyArray is defined as String instead of Boolean or even Variant?

Also, if you have several TextFields that are meant to be linked to the array, either create a control set and use the TextField’s index to match it to the array, or subclass the TextField and create a field that will hold the array index. That way you can look through the controls and set that all at once, and you can add more TextFields later pretty easily.

Thanks Kem (Bill and Christian Too)
TextfieldX. Visible = ( MyArray( 125 ) = “true” ) Works well.
Lennox

Kem’s is shorter, but also note that in 2014r1, you can use the new If operator:

TextFieldX.Visible = If(MyArray(125) = "true", True, False)

Hi Kem,
It works - TextfieldX. Visible = ( MyArray( 125 ) = “true” ) - but could you explain that for me
Thanks.
Lennox

Sure.

The “=” is doing double-duty here. In the first case, TextfieldX.Visible = ..., it is working as an assignment operator. But since there can only be one assignment in a statement, any “=” (or “<”, “>”, etc.) in the statement will work as a comparison, the same as if you used it in an If statement. The comparison always returns a Boolean, so MyArray(125) = "true" compares the value of the array to the string “true” and returns either true or false.

Since the Visible property takes a boolean anyway, the result of the comparison can be directly assigned to it.

Does that make sense?

OK Kem,

I will go over it a few times with my examples and if I still need clarification I will ask.

Thanks again.

Lennox

Oops Christian, you’re right! :slight_smile:

As far as doing it like Visible=(MyArray(125)=“true”), I prefer my method because I can then set other things to also equal true (like “yes”, as I mentioned) and it’s simple enough to change if the data source changes. It also prevents mistakes where you might type (MyArray(125)=“ture”) and don’t notice it.

Bill, yours is a fine, and very convenient, way to do it, but keep in mind that method calls take a toll on performance. It may not matter if you don’t use it very often, but it’s measurable in a big loop.

[quote=70606:@Paul Lefebvre]Kem’s is shorter, but also note that in 2014r1, you can use the new If operator:

TextFieldX.Visible = If(MyArray(125) = "true", True, False)

Paul, I could not find the new IF operator in the documentation. What is the exact syntax ?

See the LR here.

Edit (Paul): Fixed URL.

[quote=70626:@Kem Tekinay]See the LR here.

Edit (Paul): Fixed URL.[/quote]

Very informative LR indeed :wink:

[code]If
REDIRECT If…Then…Else

See Also

If…Then…Else
Category: Operator

This page was last modified on 5 February 2014, at 20:37.[/code]

It got an unnecessary edit I think. Try this:
http://documentation.xojo.com/index.php?title=If&oldid=46376

Sorry, I had to clear the cache. It should be OK now.

http://documentation.xojo.com/index.php/If