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?
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:
From MSDN in regards to Cbool (and other methods like that):
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