Answered by:
C# to VB: Converting to Boolean

Question
-
User-567435310 posted
I am converting some C# code to VB, the following code is part of a function
if (bool.Parse(v["Parent"].ToString())) { e.Row.CssClass = "Parent"; }
V is System.Data.DataRowView When I convert this to…
If Boolean.Parse(v("Parent").ToString()) Then e.Row.CssClass = "Parent" End If
The code throws an error “String was not recognized as a valid Boolean” But if I use this code
If CBool(v("Parent").ToString) = True Then e.Row.CssClass = "Parent" End If
It works fine, which is great, I am just curious why?
Thursday, July 22, 2010 11:48 AM
Answers
-
User-660870441 posted
Hi,
Bool.Prase() method only accept a specific string (“true”, “false”), if the parameter is null or other strings, it will throws that exception.
For example:
Boolean.Prase(“true”) // return true
Boolean.prase(“1”) // throw exception
Boolean.prase(Nothing) // throw exception
CBool() method works just in C language, the parameter is any object.
For example:
CBool(2) // return true
CBool(“2”) // return true
CBool(Nothing) // return true
I recommend you using Boolean.TryParse() method since it won’t throw exception.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 27, 2010 3:44 AM
All replies
-
User1224194097 posted
Convert.ToBoolean and Boolean.Parse fails to convert the value to boolean either true or false when the argument is null
So I suggest using Boolean.TryParse which checks if the argument is a valid boolean argument or not.
That way you can ensure that your code does not fail
check this link
http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx
Both are same but Tryparse does not throw an exception when conversion fails whereas Parse throws an error
Thursday, July 22, 2010 4:21 PM -
User1080785583 posted
Boolean.parse parses a string value.
CBool converts an EXPRESSION to boolean.
If Boolean.Parse(1 > 0) Then
' we are ok
'if you use OPTION SCRIPT ON and OPTION EXPLICIT ON you will get conversion error, otherwise you are ok
End If
If Boolean.Parse("string") Then
'compiler does not catch this error but will result in an error once executed
End If
Thursday, July 22, 2010 4:25 PM -
User-567435310 posted
I know that the value being passed through is not null, having debugged and stepped through the code.
If Boolean.Parse parses a string value, I am confused why this will not work. The value being passed to the function is a bit value from a SQL database and it is being passed through as a string.
Any thoughts?
Friday, July 23, 2010 3:38 AM -
User1080785583 posted
You parse like this.
http://msdn.microsoft.com/en-us/library/system.boolean.parse.aspx
Friday, July 23, 2010 8:50 AM -
User-660870441 posted
Hi,
Bool.Prase() method only accept a specific string (“true”, “false”), if the parameter is null or other strings, it will throws that exception.
For example:
Boolean.Prase(“true”) // return true
Boolean.prase(“1”) // throw exception
Boolean.prase(Nothing) // throw exception
CBool() method works just in C language, the parameter is any object.
For example:
CBool(2) // return true
CBool(“2”) // return true
CBool(Nothing) // return true
I recommend you using Boolean.TryParse() method since it won’t throw exception.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 27, 2010 3:44 AM -
User-567435310 posted
You are correct that Boolean.Parse will only accept a specific string. But CBool also works in vb.net
http://msdn.microsoft.com/en-us/library/2k9sfx3c(VS.85).aspx
Thursday, July 29, 2010 2:44 PM -
User-660870441 posted
Sorry about my typing. As you said, of course CBool works in VB.NET. I want to said is that CBool works like a bool expression in C language. It treats an integer 1 as true and a not null object as true.
If I misunderstood you, please feel free to let me know.
Thursday, July 29, 2010 9:07 PM -
User1048247244 posted
If you get stuck on a part, check out http://converter.telerik.com/. It's not 100%, but it's not bad either.
Friday, August 13, 2010 4:40 PM