Question about an old operator

M

Manuel

Hi,

I have a problem regarding vb6 LIKE operator patterns

The patterns are inputed by the final user and I have to use it to compare
strings.

vb6 has a LIKE operator, it's obsolete cause regular expressions are more
powerful to do the same job
vb .net has the same operator (brobabilly to help the upgrade from the old
language)

My problem is that the LIKE patterns are not fixed in the program but are
inputed directly by the final user so I have to
reproduce the exact behavior of this operator in a C# program.

In order to save time I can use vb .net to write something like this:

Shared Function vbLike(ByVal stringa As String, ByVal pattern As String) As
Boolean
Return stringa Like pattern
End Function

compile into a class library and reference it in my C# program...
I ask if there is another solution, maybe somethig usable from in the
Microsoft.VisualBasic Namespace

thank you very much
 
B

Ben Voigt [C++ MVP]

Compile that code in VB.NET

Use .NET Reflector to view the assembly, select C# view to see the
equivalent C# code (which I suspect will call the VB runtime support library
just like you suggested).
 
M

Manuel

Thank you very much Ben Voigt

Red Gate's .NET Reflector is a very useful tool and I never heard about it
before.

It compiles

public static bool vbLike(string str, string pattern)
{
return LikeOperator.LikeString(str, pattern, CompareMethod.Binary);
}

LikeOperator is in the Microsoft.VisualBasic.CompilerServices namespace but
I can't import it into a C# program
 
H

Hans Kesting

Manuel used his keyboard to write :
Hi,

I have a problem regarding vb6 LIKE operator patterns

The patterns are inputed by the final user and I have to use it to compare
strings.

vb6 has a LIKE operator, it's obsolete cause regular expressions are more
powerful to do the same job
vb .net has the same operator (brobabilly to help the upgrade from the old
language)

My problem is that the LIKE patterns are not fixed in the program but are
inputed directly by the final user so I have to
reproduce the exact behavior of this operator in a C# program.

In order to save time I can use vb .net to write something like this:

Shared Function vbLike(ByVal stringa As String, ByVal pattern As String) As
Boolean
Return stringa Like pattern
End Function

compile into a class library and reference it in my C# program...
I ask if there is another solution, maybe somethig usable from in the
Microsoft.VisualBasic Namespace

thank you very much

Maybe you can change that like-expression into a regex:
"?" -> "."
"*" -> ".*"
"#" -> "[0-9]"
"[..]" can remain
"[!..]" -> "[^..]"
"\" -> "\\" (but watch out, you might need "\\\\" or @"\\" in C#)

like operator:
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

regex syntax:
http://msdn.microsoft.com/en-us/library/20bw873z.aspx
http://msdn.microsoft.com/en-us/library/az24scfc.aspx

Hans Kesting
 
B

Ben Voigt [C++ MVP]

Manuel said:
Thank you very much Ben Voigt

Red Gate's .NET Reflector is a very useful tool and I never heard
about it before.

It compiles

public static bool vbLike(string str, string pattern)
{
return LikeOperator.LikeString(str, pattern, CompareMethod.Binary);
}

LikeOperator is in the Microsoft.VisualBasic.CompilerServices
namespace but I can't import it into a C# program

Did you just add a using statement or did you also add an assembly
reference?
 
A

Arne Vajhøj

Manuel said:
Thank you very much Ben Voigt

Red Gate's .NET Reflector is a very useful tool and I never heard about it
before.

It compiles

public static bool vbLike(string str, string pattern)
{
return LikeOperator.LikeString(str, pattern, CompareMethod.Binary);
}

LikeOperator is in the Microsoft.VisualBasic.CompilerServices namespace but
I can't import it into a C# program

using System;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(LikeOperator.LikeString("ABC", "A*",
CompareMethod.Binary));
Console.ReadKey();
}
}
}

with ref to Microsoft.VisualBasic.dll works for me.

Arne
 
L

Lingzhi Sun [MSFT]

Hi Manuel,

Thank you for using Microsoft Newsgroup Support Service. My name is
Lingzhi Sun [MSFT]. It's my pleasure to work with you on this case.

Also thanks to Ben, Hans and Arne for your great ideas.

Although LikeOperator is under the namespace of
Microsoft.VisualBasic.CompilerServices, we can add a reference to the
Microsoft.VisualBasic assembly into our C# project, so that all the public
classes and methods inside the assembly can be used. To add the
Microsoft.VisualBasic assembly as reference, we can follow these steps:
Project References --> Add Reference… --> Under ".NET" tab, select
"Microsoft.VisualBasic" --> Press "OK". Then LikeOperator.LikeString
method can be used in C# codes as Arne's post suggested.

If you don't want to add the assembly Microsoft.VisualBasic.dll into your
C# project, you can consider realizing your own method based on the source
codes of LikeOperator.LikeString method. Reflector is one of the best
tools to see the implementation of the Microsoft.VisualBasic assembly.
Please open the file Microsoft.VisualBasic.dll in Reflector. The file is
under the folder of "%SystemRoot%\Micorosft .NET\Framework\v2.0.50727\".
This method is more difficult to realize than directly calling the method
LikeOperator.LikeString, because the implementation of this method refers
to many other classes and methods in Microsoft.VisualBasic assembly.

Another approach is to convert the pattern of LIKE operator to the pattern
of regular expression. You can refer to Hans' post. Also I provide you
with a code snippet for reference, please modify these codes to fulfill
your own target.
=======================================
private static bool vbLikeRegax(string str, string pattern)
{
pattern = pattern.Replace("!", "^")
.Replace("?", ".").Replace(@"\", @"\\")
.Replace("#", @"\d");
Regex reg = new Regex(pattern);
foreach (Match m in reg.Matches(str))
{
if (m.Success)
if (m.Length == str.Length)
return true;
}
return false;
}
=======================================

Important to note: Generally, regular expression is worse in performance
than the method LikeOperator.LikeString. The source code of
LikeOperator.LikeString tells us that it uses its own rule to parse the
string instead of using .NET Regex class. This rule is much simpler than
the rule of regular expression when parsing the pattern string. More
detailed information on LikeOperator can be found in this link:
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx. For the additional
information on regular expression in .NET, please see
http://msdn.microsoft.com/en-us/library/hs600312.aspx.

Here is another article which discusses the performance of regular
expression and the LikeOperator.LikeString method:
http://www.dotnet2themax.com/blogs/fbalena/PermaLink,guid,53da27bf-b980-4abc
-a4e8-a452dc6ceb26.aspx.


If you have any other questions, please be free to let me know. Have a
great day!


Regards,
Lingzhi Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top