# CBool in Xojo

Hello, CBool Operator does not exist in Xojo, is there an equivalent operator to convert this code ?

``````  Dim Char As String

If CBool(Sgn(dd) - 1) And Lat Then
Char = "S"
ElseIf CBool(Sgn(dd) - 1) Then
Char = "E"
ElseIf Lat Then
Char = "N"
Else
Char = "W"
End If``````

Try this (untested):

[code]
Dim Char As String

If if(Sgn(dd) - 1=0,false,true) And Lat Then
Char = “S”
ElseIf if(Sgn(dd) - 1=0,false,true) Then
Char = “E”
ElseIf Lat Then
Char = “N”
Else
Char = “W”
End If[/code]

``````  Dim Char As String

If ((Sgn(dd) - 1)<>0) And Lat Then
Char = "S"
ElseIf ((Sgn(dd) - 1)<>0) Then
Char = "E"
ElseIf Lat Then
Char = "N"
Else
Char = "W"
End If``````

why not simply CType for boolean?

Or go through a Variant.

I’ve been told that variants are exceptionally slow. I’ve not confirmed that, but various engineers have said that over the years. Be a good experiment when I have spare time.

Maybe Im missing something but isnt

``CBool(Sgn(dd) - 1)``

just an obfuscated version of

``dd <= 0``

?

The not obvious, but optimized version should be:

``````  Dim Char As String

If (dd <= 0) And Lat Then
Char = "S"
ElseIf (dd <= 0) Then
Char = "E"
ElseIf Lat Then
Char = "N"
Else
Char = "W"
End If``````

But the answer to the OP question is:

What is the equivalent to CBool(x) for a numeric x?

the overhead of variant is not that big that you should worry.
Of course if you start using variant everywhere instead of integers for example, your performance will suffer.
On a variant, for each value assignment, an object is created. For every access, there may be a conversion needed which can also hurt on performance.

``````Dim Char As String

If ((Sgn(dd) - 1)<>0) And Lat Then
Char = "S"
ElseIf ((Sgn(dd) - 1)<>0) Then
Char = "E"
ElseIf Lat Then
Char = "N"
Else
Char = "W"
End If``````

with this code it’s Ok, thank’s every body.

``````if (Sgn(dd) - 1) <> 0 then
if Lat then Char = "S" else Char = "E"
else
if Lat then Char = "N" else Char = "W"
end``````

Or with the new If construct:

``````if (Sgn(dd) - 1) <> 0 then
Char = If( Lat, "S", "E" )
else
Char = If( Lat, "N", "W" )
end``````

Or even:

``Char = If( ( Sgn( dd ) - 1 ) <> 0, If( Lat, "S", "E" ), If( Lat, "N", "W" ) )``

(I don’t recommend that last one.)

It was interesting. Many minds, one problem, a huge clean up.

``````if dd <= 0 then
Char = If( Lat, "S", "E" )
else
Char = If( Lat, "N", "W" )
end``````

Right sgn(dd) returns -1, 0, or 1. so CBool(sgn(dd)-1) true means that sgn(dd) is not 1. Therefore dd was either 0 or < 0. How convoluted can you get?

To be true, I wrote a simplified version of this in a piece of paper after reading Michael H’s argument.
Just to be sure.