Jul 29, 2016 Replies: 10 SubName: BooleanConv. Descriptions: Converts a Boolean to an Int or vice-versa Return value: Object: Int(0 or 1) if Value is a Boolean - Boolean(False or True) if Value is an Int Code: Public Sub BooleanConv(Value As Object) As Object Dim Result As Object = 0 If GetType(Value) = "java.lang.Integer" Then Dim b As Boolean = (Value = 1) Result = b Else ' value should be a boolean If Value = True Then Result = 1 End If End If Return Result End Sub Example: Code: Dim Bool As Boolean Bool = False Log("conv Bool = False: " & BooleanConv(Bool)) Bool = True Log("conv Bool = True: " & BooleanConv(Bool)) Dim Integer As Int Integer = 0 Log("conv Integer = 0: " & BooleanConv(Integer)) Integer = 1 Log("conv Integer = 1: " & BooleanConv(Integer)) LOG: conv Bool = False: 0 conv Bool = True: 1 conv Integer = 0: false conv Integer = 1: true NOTE: Sorry, I know that it isn't exactly the "silver bullet" but it can be useful Tags: Boolean, Convert, Conversion Last edited: Jul 29, 2016 Click to expand...
SubName: BooleanConv. Descriptions: Converts a Boolean to an Int or vice-versa Return value: Object: Int(0 or 1) if Value is a Boolean - Boolean(False or True) if Value is an Int Code: Public Sub BooleanConv(Value As Object) As Object Dim Result As Object = 0 If GetType(Value) = "java.lang.Integer" Then Dim b As Boolean = (Value = 1) Result = b Else ' value should be a boolean If Value = True Then Result = 1 End If End If Return Result End Sub Example: Code: Dim Bool As Boolean Bool = False Log("conv Bool = False: " & BooleanConv(Bool)) Bool = True Log("conv Bool = True: " & BooleanConv(Bool)) Dim Integer As Int Integer = 0 Log("conv Integer = 0: " & BooleanConv(Integer)) Integer = 1 Log("conv Integer = 1: " & BooleanConv(Integer)) LOG: conv Bool = False: 0 conv Bool = True: 1 conv Integer = 0: false conv Integer = 1: true NOTE: Sorry, I know that it isn't exactly the "silver bullet" but it can be useful Tags: Boolean, Convert, Conversion
wonder Expert Licensed User Longtime User Jul 29, 2016 #2 Why not just: Code: Sub Bool2Int(value as Boolean) as Int If value Then Return 1 Return 0 End Sub Sub Int2Bool(value as Int) as Boolean If value = 0 Then Return False Return True End Sub
Why not just: Code: Sub Bool2Int(value as Boolean) as Int If value Then Return 1 Return 0 End Sub Sub Int2Bool(value as Int) as Boolean If value = 0 Then Return False Return True End Sub
LucaMs Expert Licensed User Longtime User Jul 29, 2016 #3 wonder said: Why not just: Code: Sub Bool2Int(value as Boolean) as Int If value Then Return 1 Return 0 End Sub Sub Int2Bool(value as Int) as Boolean If value = 0 Then Return False Return True End Sub Click to expand... To have a single routine Also, it's easy to remember, given its name.
wonder said: Why not just: Code: Sub Bool2Int(value as Boolean) as Int If value Then Return 1 Return 0 End Sub Sub Int2Bool(value as Int) as Boolean If value = 0 Then Return False Return True End Sub Click to expand... To have a single routine Also, it's easy to remember, given its name.
wonder Expert Licensed User Longtime User Jul 30, 2016 #4 Your method is fine Luca. However, in a performance critical application (games, physics, simulations) the creation and destruction of a Java Object is a more expensive operation than simply dealing with primitive types.
Your method is fine Luca. However, in a performance critical application (games, physics, simulations) the creation and destruction of a Java Object is a more expensive operation than simply dealing with primitive types.
LucaMs Expert Licensed User Longtime User Jul 30, 2016 #5 wonder said: Your method is fine Luca. However, in a performance critical application (games, physics, simulations) the creation and destruction of a Java Object is a more expensive operation than simply dealing with primitive types. Click to expand... You're right but I need it for DB projects
wonder said: Your method is fine Luca. However, in a performance critical application (games, physics, simulations) the creation and destruction of a Java Object is a more expensive operation than simply dealing with primitive types. Click to expand... You're right but I need it for DB projects
MaFu Well-Known Member Licensed User Longtime User Aug 1, 2016 #6 But instead of Code: Dim b As Boolean = (Value = 1) i would use Code: Dim b As Boolean = (Value <> 0)
But instead of Code: Dim b As Boolean = (Value = 1) i would use Code: Dim b As Boolean = (Value <> 0)
LucaMs Expert Licensed User Longtime User Aug 1, 2016 #7 MaFu said: But instead of Code: Dim b As Boolean = (Value = 1) i would use Code: Dim b As Boolean = (Value <> 0) Click to expand... No, because you should pass 1 or 0 only (checks are not performed in the routine, but it is a simple little thing that has a simple purpose).
MaFu said: But instead of Code: Dim b As Boolean = (Value = 1) i would use Code: Dim b As Boolean = (Value <> 0) Click to expand... No, because you should pass 1 or 0 only (checks are not performed in the routine, but it is a simple little thing that has a simple purpose).
LucaMs Expert Licensed User Longtime User Aug 1, 2016 #8 It should have also: If Value <> 0 And Value <> 1 And Value <> False And Value <> True Then ' raise an exception Last edited: Aug 1, 2016
It should have also: If Value <> 0 And Value <> 1 And Value <> False And Value <> True Then ' raise an exception
MaFu Well-Known Member Licensed User Longtime User Aug 1, 2016 #9 LucaMs said: No, because you should pass 1 or 0 only (checks are not performed in the routine, but it is a simple little thing that has a simple purpose). Click to expand... Maybe. In this case it doesn't matter, so why not use "Value <> 0" and use the safety way.
LucaMs said: No, because you should pass 1 or 0 only (checks are not performed in the routine, but it is a simple little thing that has a simple purpose). Click to expand... Maybe. In this case it doesn't matter, so why not use "Value <> 0" and use the safety way.
LucaMs Expert Licensed User Longtime User Aug 1, 2016 #10 MaFu said: Maybe. In this case it doesn't matter, so why not use "Value <> 0" and use the safety way. Click to expand... Because it wouldn't be a safety way; the only right way should be to add checks
MaFu said: Maybe. In this case it doesn't matter, so why not use "Value <> 0" and use the safety way. Click to expand... Because it wouldn't be a safety way; the only right way should be to add checks
MaFu Well-Known Member Licensed User Longtime User Aug 1, 2016 #11 This is because you would use only 0 and 1. Several programming languages interpretes 0 as false and all other values as true. And i can use this routine also this way, but not with your implementation. If only 0 and 1 should be allowed then more checks are needed. In this point i agreed with you. But for a simple check or maybe a more "fuzzy" comparison is the "<> 0" check the right way.
This is because you would use only 0 and 1. Several programming languages interpretes 0 as false and all other values as true. And i can use this routine also this way, but not with your implementation. If only 0 and 1 should be allowed then more checks are needed. In this point i agreed with you. But for a simple check or maybe a more "fuzzy" comparison is the "<> 0" check the right way.