Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.");
}
like image 420
Moslem Ben Dhaou Avatar asked Apr 25 '26 20:04

Moslem Ben Dhaou


2 Answers

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)

like image 132
Marc Gravell Avatar answered Apr 27 '26 11:04

Marc Gravell


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.");
}
like image 30
Siva Gopal Avatar answered Apr 27 '26 12:04

Siva Gopal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!