views:

424

answers:

0

I have a requirement whereby I have to implement the masking provided by MaskedTextBox, but without using that UI component in the business layer or say for doing CLR integration in SQL Server 2005 (so that I can implement a db function that invokes my C# method).

However there is some anomaly between the behavior of the MaskedTextBox component and the MaskedTextProvider class. I have tried my best to code a workaround (by doing string/char manipulation etc.) but I am at a loss as to why should the two behave differently. It will be great if someone can explain the output of the following SAMPLE code (I have pasted the output too):

// Sample Code Begins

// This requires a Windows Console application. To compile the code we should // add reference to System.Windows.Forms assembly

class Program
{

    static void Main(string[] args) {

        int caseIdx = 0;
        FormatNumber(++caseIdx, "(+00) 00000 00000", "919840278798");
        FormatNumber(++caseIdx, "(+\\9\\1) 00000 00\\§00", "9840278798");
        FormatNumber(++caseIdx, "(00) AAaa CCL0", "121 %''D89*(435t6");
        FormatNumber(++caseIdx, "(00) 000« ''\\u0000\\u", "98   8");
        Console.ReadLine();

    }

    static void FormatNumber(int caseIdx, string mask, string number) {

        MaskedTextBox maskTextBox = new MaskedTextBox();
        maskTextBox.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
        maskTextBox.Mask = mask;
        maskTextBox.Text = number;

        MaskedTextProvider provider = new MaskedTextProvider(mask);
        provider.Set(number);

        if (maskTextBox.Text == provider.ToDisplayString())
            Console.WriteLine(".");
        else
            Console.WriteLine("F\t # " + caseIdx + " " + maskTextBox.Text + " " + provider.ToDisplayString() + " " + (maskTextBox.Text == provider.ToDisplayString()));

    }

}

// Sample Code Ends

// Output Begins

.

F # 2 (+91) 98402 78§79 (+91) _ § False

F # 3 (12) 1_D8 9*t6 () __ ____ False

.

// Output Ends

As you can see, the first and the fourth case succeeds (meaning the output obtained from the MaskedTextBox is the same as that obtained from MaskedTextProvider, and that is why we see a dot for those cases) where as the other two fails.

Can anyone throw some light as to why this is so and what can be done/explored to set this right? Any help is truly appreciated.

PS: Of course, if I am ready to use the MaskedTextBox directly, instead of MaskedTextProvider, this problem would disappear. But I am not sure whether it is a good idea to use a UI component in either the business layer or for SQL Server CLR integration.