4

If I try to put a string into a Boolean variable such as this:

    Dim testValue As String = "True"
    Dim bool1 As Boolean = testValue

With Option Strict On I get an error and the suggested fix is to change the second line to:

    Dim bool1 As Boolean = CBool(testValue)

This is fine, But - what are the advantages / disadvantages of doing this instead:

    Dim bool1 As Boolean = Boolean.Parse(testValue)

CBool feels very VB6 like to me but which should we be using and why?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143

4 Answers4

5

If you know its a string in both cases, it should be an equivalent process. Since Cbool will eventually call a function to convert it. (As long as your value is "True" or "False")

There is a difference if you use something like cbool(value) and the value is a boolean.

from MSDN:

Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.

From MSDN in regards to Cbool (and other methods like that):

These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression. Sometimes there is no call to a procedure to accomplish the conversion, which improves performance. Each function coerces an expression to a specific data type

So, if you use cbool(value) if your value is a boolean it just uses it, no conversion required. That makes it potentially more efficient.

You can check this out too: Integer.Parse vs. CInt

Community
  • 1
  • 1
Jay
  • 5,897
  • 1
  • 25
  • 28
1

I think the big difference is that Boolean.Parse is very strict about what it will accept (true/false) where as CBool is more lenient (0/1, true/false, I think yes/no although I'd have to retest that).

If you are ever going to port the code to C# or know that you will only ever have true/false values, then Parse would be the way to go. Otherwise, I would use CBool for its added flexibility.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

CBool(value) is a cast operator, with this method, value is supposed to be a Bool, but VB do some convertions for you.

With Bool.Parse(value), value is supposed to be a String, and it will be parsed to a Boolean value.

You can use Convert.ToBoolean(value) too, which use the convertion methods of .net to perform a convertion from one type to another.

In you code, is incorrect to cast a empty string to a false value. It is possible, but error prone.

Using Strict in VB.Net, you are forced to use the correct type.

So, you can't do this:

Dim v1 as String = "1"
Dim v2 as Integer = 1

Dim r = v1 + v2

r could be 2 or "11", and this kind of error is very common.

sergiogarciadev
  • 2,061
  • 1
  • 21
  • 35
0

This seems to show that in the case of string to boolean, Boolean.Parse is much faster.

    Dim t As String = "True"
    Dim f As String = "False"
    Const tries As Integer = 1000000
    Dim t_or_f As Boolean = True
    Dim stpw As New Stopwatch

    stpw.Restart()
    For x As Integer = 1 To tries
        t_or_f = CBool(f)
    Next
    stpw.Stop()
    Debug.WriteLine(stpw.Elapsed)

    stpw.Restart()
    For x As Integer = 1 To tries
        t_or_f = Boolean.Parse(f)
    Next
    stpw.Stop()
    Debug.WriteLine(stpw.Elapsed)

When I see these types of questions I can't help but wonder why / how a programmer is in the situation of converting some data into a Boolean directly. As close as I can come to acceptance of this is that the numeric value of 1 = True and 0 = False, but even then you are faced with the fact that .Net interprets Boolean differently.

    Dim t_or_f As Boolean = CBool(1)
    Debug.WriteLine(t_or_f.ToString & " " & CLng(t_or_f))
    t_or_f = CBool(0)
    Debug.WriteLine(t_or_f.ToString & " " & CLng(t_or_f))

debug output

True -1
False 0
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • 1) .Parse is quicker on a string but CBool is a lot quicker on a Boolean and is doing more. 2) How about the case where you are reading some values from a text file that happen to the Boolean? The writing of the text file may be out of your control. 3) .Net defines False as Zero and True as Non-zero (default -1) so the output is as expected I would say. – Matt Wilko Dec 14 '11 at 11:16