Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB Like Operator does not the same as Operators.LikeString()

Tags:

vb.net

Why does in the following sample the first (x) be true and the second (y) be false? I want to use the VB-Like Operator in C#.

Dim x = "[email protected]" Like "*symas-design.ch"
Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("[email protected]", "*symas-design.ch", CompareMethod.Text)
like image 941
BennoDual Avatar asked Jan 04 '14 22:01

BennoDual


1 Answers

The Like operator apparently doesn't use Operators.LikeString, but instead LikeOperator.LikeString.

I tried this LINQPad program:

Sub Main
    Dim x = "[email protected]" Like "*symas-design.ch"
    Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("[email protected]", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)

    x.Dump()
    y.Dump()
End Sub

And it showed:

True
False

so as you say, they don't do the same, but then I showed the IL generated:

IL_0000:  ldstr       "[email protected]"
IL_0005:  ldstr       "*symas-design.ch"
IL_000A:  ldc.i4.0    
IL_000B:  call        Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString
IL_0010:  stloc.0     // x
IL_0011:  ldstr       "[email protected]"
IL_0016:  ldstr       "*symas-design.ch"
IL_001B:  ldc.i4.1    
IL_001C:  call        Microsoft.VisualBasic.CompilerServices.Operators.LikeString
IL_0021:  stloc.1     // y
IL_0022:  ldloc.0     // x
IL_0023:  call        LINQPad.Extensions.Dump
IL_0028:  pop         
IL_0029:  ldloc.1     // y
IL_002A:  call        LINQPad.Extensions.Dump

And it seems they're calling different methods, so then I tried this:

Sub Main
    Dim x = "[email protected]" Like "*symas-design.ch"
    Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("[email protected]", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)
    Dim z = Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString("[email protected]", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)

    x.Dump()
    y.Dump()
    z.Dump()
End Sub

And I got:

True   <-- Like operator
False  <-- Operator.LikeString
True   <-- LikeOperator.LikeString

so you need to use LikeOperator.LikeString instead.

Here's a C# LINQPad program that demonstrates:

void Main()
{
    bool z = LikeOperator.LikeString("[email protected]", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text);
    z.Dump();
}

Which also outputs True.

like image 64
Lasse V. Karlsen Avatar answered Nov 09 '22 02:11

Lasse V. Karlsen