A way to get "Like" to use "Option Compare Text" without changing the setting for all my code?

Discussion in 'Microsoft VB .NET' started by Linda, Mar 29, 2005.

  1. Linda

    Linda Guest

    Hi,

    Is there a way to do a "text" (rather than "binary") compareison with
    the "like" operator, without changing the global "Option Compare"
    setting? I don't want to risk breaking many, many lines of functional
    code just to get one "like" operation to behave as I wish.

    I want to check whether a single-character string is (a letter or
    number, INCLUDING diacritical letters) or whether it is (something
    else.)

    if str1$ like "[A-Z]" or str1$ like "#" then
    'blah blah blah
    else
    'blah blah blah
    end if

    ....would work if I changed the source file's option to "text", but as I
    said, I don't want to risk breaking the rest of the code. It's the need
    to include diacritical letters in that makes this complicated,
    otherwise I could just add an [a-z] comparison.

    Is there another way to do this, or a way to make this work?

    Thanks.
     
    Linda, Mar 29, 2005
    #1
    1. Advertisements

  2. Linda

    Linda Guest

    In case anyone is interested / faces a similar problem, here's how I
    solved it:

    In the library that I use for most of my applications, I created a
    "TextCompare" class in it's own source file, which is Option Compare
    Text. The class consists of public shared functions that simply invoke
    the "like" operator and other comparison checks that reply on "Option
    Compare".

    (For my other source files, I've added an explicit "Option Compare
    Binary" in addition to the obligitory "Option Strict On" and "Option
    Explicit On" just so it's unambigous what's happening).

    Now, if I want "Compare Binary" I do the comparison the standard way.
    If I want "Compare Text" (and String.Compare() isn't appropriate) I
    call the TextCompare static method.

    =======================================================

    Option Strict On
    Option Explicit On
    Option Compare Text

    Public Class TextCompare

    Public Shared Function txtEquals(ByVal str1$, ByVal str2$) _
    As Boolean
    Return str1$.Equals(str2$)
    End Function

    Public Shared Function txtLike(ByVal str1$, ByVal pattern$) _
    As Boolean
    If str1$ Like pattern$ Then Return True
    Return False
    End Function

    Public Shared Function indexOf(ByVal outerStr$, ByVal innerStr$) _
    As Integer
    Return outerStr$.IndexOf(innerStr$)
    End Function

    Public Shared Function lastIndexOf(ByVal outerStr$, ByVal
    innerStr$) _
    As Integer
    Return outerStr$.LastIndexOf(innerStr)
    End Function

    Public Shared Function startsWith(ByVal outerStr$, ByVal innerStr$)
    _
    As Boolean
    Return outerStr$.StartsWith(innerStr)
    End Function

    Public Shared Function endsWIth(ByVal outerStr$, ByVal innerStr$) _
    As Boolean
    Return outerStr$.EndsWith(innerStr)
    End Function

    End Class
     
    Linda, Apr 1, 2005
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William H. Burling
    Replies:
    1
    Views:
    188
    Jay B. Harlow [MVP - Outlook]
    Sep 27, 2003
  2. Niels

    get index of combobox (compare text)

    Niels, May 10, 2004, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    374
    Herfried K. Wagner [MVP]
    May 10, 2004
  3. Samuel R. Neff
    Replies:
    1
    Views:
    327
    Peter Huang [MSFT]
    Jan 28, 2005
  4. Guest

    Compare Files like SourceSafe

    Guest, Oct 20, 2005, in forum: Microsoft VB .NET
    Replies:
    8
    Views:
    844
  5. Michael Jenck

    CodeDOM Help for Option Compare

    Michael Jenck, Apr 8, 2007, in forum: Microsoft VB .NET
    Replies:
    0
    Views:
    422
    Michael Jenck
    Apr 8, 2007
Loading...

Share This Page