Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

How is this VB.NET code evaluated?

I am working on a code conversion from VB.NET to C#. I found this piece of code which I am trying to wrap my head around but I can't get how it is evaluated:

If eToken.ToLower = False Then _
    Throw New Exception("An eToken is required for this kind of VPN-Permission.)

My problem is with the comparison between a string eToken.ToLower and a boolean value False.

I tried to use a converter and what I got was as follow (which is not a valid statement in C# as you cannot compare a string to a bool):

if (eToken.ToLower() == false) {
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}

Upvotes: 3

Views: 153

Answers (2)

Siva Gopal
Siva Gopal

Reputation: 3502

You can do a type cast assuming, eToken has a value of "true"/"false":

if (Convert.ToBoolean(eToken.ToLower())==false)
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062915

I compiled it and decompiled the IL; in C# that is:

string eToken = "abc"; // from: Dim eToken As String = "abc" in my code 
if (!Conversions.ToBoolean(eToken.ToLower()))
{
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}

So there's your answer: it is using Conversions.ToBoolean (where Conversions is Microsoft.VisualBasic.CompilerServices.Conversions in Microsoft.VisualBasic.dll)

Upvotes: 4

Related Questions