Testing a Comparer instance type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luc The Perverse

    #1

    Testing a Comparer instance type

    Hello!

    I am trying to find a way to deal with Windows' case insensitivity
    without just forcing everything lowercase. While I am open to criticism
    on my method - my question relates to the fourth to bottom line (not
    including curly braces) on the bottom - testing to see if the Comparer
    is not StringComparer. OrdinalIgnoreCa se

    My concern is eventually when I decide to serialize these to files, to
    store persistant data, that somehow - either through usage,
    implementation or design change that the type of the StringComparer will
    get changed.

    For simplicity sake, I have removed error handling and warnings.



    public abstract class Task{ //A class to facilitate error handling and
    progress for blocking calls
    public Dictionary<stri ng, Dictionary<stri ng, string>data;
    protected abstract void execute();
    public void ExecuteInBlocki ngCall(){
    execute();
    }
    }

    public class InitializeCaseI nsensitiveFileS torage : Task { //prepare
    dictionaries to hold case insensitive local files
    protected override void execute() {
    string[] alwaysDelete = {"new_dest", "new_hash", "new_date"} ;
    string[] alwaysCheck = {"old_date", "old_hash", "old_dest"} ;
    foreach(string i in alwaysDelete){
    if(data.Contain sKey(i))
    data.Remove(i);
    data.Add(i, new Dictionary<stri ng,
    string>(StringC omparer.Ordinal IgnoreCase));
    }
    foreach(string i in alwaysCheck)
    if(!data.Contai nsKey(i))
    data.Add(i, new Dictionary<stri ng,
    string>(StringC omparer.Ordinal IgnoreCase));
    else if(data[i].Comparer!=Stri ngComparer.Ordi nalIgnoreCase){
    data.Remove(i);
    data.Add(i, new Dictionary<stri ng,
    string>(StringC omparer.Ordinal IgnoreCase));
    }
    }


    Is data[i].Comparer!=Stri ngComparer.Ordi nalIgnoreCase a reasonable way
    to check and make sure that we are using the expected string comparer?
    Will this work even after serialization? (I will add [Serializable]
    tags and have this handled automatically)

    Thank you

    --
    LTP

    :)
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Testing a Comparer instance type

    Hi,

    if(data[i].Comparer!=Stri ngComparer.Ordi nalIgnoreCase){
    The above line might be incorrect, according to MSDN it returns an instance
    of an anonymous class. It does not says if it will ALWAYS return the same
    isntance.




    Ignacio Machin
    The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

    Mobile & warehouse Solutions.


    Comment

    • Luc The Perverse

      #3
      Re: Testing a Comparer instance type

      Ignacio Machin ( .NET/ C# MVP ) wrote:
      Hi,
      >
      >
      >if(data[i].Comparer!=Stri ngComparer.Ordi nalIgnoreCase){
      >
      The above line might be incorrect, according to MSDN it returns an instance
      of an anonymous class. It does not says if it will ALWAYS return the same
      isntance.
      Ok.

      This is what I was worried about

      Is there anyway I can test it short of adding something to the
      dictionary and the making sure I find it again with capitalization
      changed? I'm trying to avoud *that* method if possible.

      --
      LTP

      :)

      Comment

      Working...