micbobo
micbobo

Reputation: 872

How is a string value used as a boolean

I was playing around with my code in VB.NET (Visual Studio IDE) when I realised that I could affect a boolean value into a string

The string consequently took the value "True" or "False"

I then tried using it as a boolean such as

If StringValueContainingTrueOrFalse then
    'Do Something
End if

This also worked and is giving the desired result. This made me realise how little I knew about how things worked in the background.

Is the word True in the string being detected and the IDE is smart enough to deal with it or does it simply try to convert the value to what it needs to be (so knowing that it needs a boolean value try to convert the string into one to do it's actions) ?

What is happening making this possible ?

Upvotes: 4

Views: 2385

Answers (5)

YaoChen Aan
YaoChen Aan

Reputation: 19

This is a boolean value: MyString.Contains("true") as in: if MyString.Contains("true") then

Upvotes: -1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

When a non Boolean expression is used in the IF statement, Vb.Net compiler will convert the expression to Boolean using Conversions.ToBoolean method.

Your code is equal to

If Conversions.ToBoolean(StringValueContainingTrueOrFalse) then
    'Do Something
End if

If your value could be converted to a Boolean, all is well. Otherwise exception will be thrown.

For example:

Sub Main
    Dim StringValueContainingTrueOrFalse as String = "True"

    IF StringValueContainingTrueOrFalse then
    Console.WriteLine("true")
    end if  
End Sub

Above program generates the following IL:

IL_0000:  ldstr       "True"
IL_0005:  stloc.0     // StringValueContainingTrueOrFalse
IL_0006:  ldloc.0     // StringValueContainingTrueOrFalse
IL_0007:  call        Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean
IL_000C:  brfalse.s   IL_0018
IL_000E:  ldstr       "true"
IL_0013:  call        System.Console.WriteLine
IL_0018:  ret

You can see the label IL_0007 shows the call to Conversions.ToBoolean method.

Upvotes: 2

Ewan
Ewan

Reputation: 1310

VB expects a Boolean in the If block so it automatically tries to cast whatever your expression is to a Boolean using implicit conversion.

The Boolean casting recognizes true/false strings and correctly parses them.

This is normally considered risky. try adding

Option Strict On

Also see the MSDN documentation for VB.Net If statement :

https://msdn.microsoft.com/en-us/library/752y8abs.aspx

condition Required. Expression. Must evaluate to True or False, or to a data type that is implicitly convertible to Boolean.

Upvotes: 2

Shar1er80
Shar1er80

Reputation: 9041

When a String is directly used in an if statment, it'll try to convert it to a Boolean so it can perform the evaluation. "True" or "False" can be converted to Booleans but other values cannot.

Module Module1
    Sub Main()
            Dim StringValueContainingTrueOrFalse As String = "True"
        If StringValueContainingTrueOrFalse Then
            Console.WriteLine(StringValueContainingTrueOrFalse)
        End If

        Console.ReadLine()
    End Sub
End Module

Results:

enter image description here

So to answer your question as to what's happening, there is a "behind the scenes" conversion that is happening.

enter image description here

Upvotes: 1

James Thorpe
James Thorpe

Reputation: 32212

It's not the IDE doing it, rather it's doing a runtime cast from one type to another. If the string contains a value that can be cast to a boolean, you're fine. If not, you'll get a runtime error.

I would recommend using Option Strict On to get compile time errors instead, so you don't unintentionally rely on runtime casting you're not aware of. If you turn this option on, the code in your question won't compile.

Upvotes: 4

Related Questions