The most stupid error translation…

My MSSQL Server was installed with polish localization. And an error occured when a XML file had been loaded to the database:

Nieprawidłowa wartość znakowa dla specyfikacji obsady

WTF? It’s in Polish. My mother language is Polish. But I completely don’t understand that gibberish.
With help of Google Translate (Polish > English) I found the original error:

Invalid character value for cast specification

and it’s simple, clear and understandable.

Hurray! An hour spent on nothing, erm… sorry, on googling…

WP8 emulator doesn’t start

Problem:

The Windows Phone Emulator wasn’t able to create a differencing disk:

The process cannot access the file ‘C:\Path\To\VHD\dd.480×800. xxx.vhd’ because it is being used by another process.

Remedy (in my silly case of course):
Exit from project’s ‘bin’ dir that was opened in Total Commander…

Position of the cursor and TextChanged event

Two additional lines are needed if we want to make cursor stay at the end of the line when editing content of TextBox in TextChanged event.

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox txtb = (sender as TextBox);
    // remember cursor location
    int loc = txtb.SelectionStart;
    // do something with txtb.Text;
    txtb.Text = txtb.Text.Replace('$','%');
    // set old location
    txtb.SelectionStart = loc;
}

TextBoxChanged in WP7 always firing twice per change

And it is annoying when you want to implement a custom validation for a simple control. So what is the problem and remedy for it?

Problem: Template for TextBox in WP7 contains… two TextBoxes. One for the enabled state and the other one for disabled / read only state.

Remedy: If you really know that you won’t be using other states than enabled state you can simply remove DisabledOrReadonlyBorder and all code that using this border. For a second option you can change the second TextBox with e.g. an Rectangle and fill it with some semitransparent brush.

Check if string contains any chinese or russian characters

The two really helpful extension methods for this task:

private static readonly Regex regexChinese = new Regex(@"\p{IsCJKUnifiedIdeographs}");
public static bool IsChinese(this char s)
{
     return regexChinese.IsMatch(s.ToString());
}

private static readonly Regex regexCyrlics = new Regex(@"\p{IsCyrillic}");
public static bool IsCyrylic(this char s)
{
     return regexCyrlics.IsMatch(s.ToString());
}

We put the code to a static class and then simply write:

if (sometext.Any(z=>z.IsChinese() || z.IsCyrillic()))
     DoSomething();

You can do the same thing for some other alphabets. Check MSDN.

Difference in capturing groups between Java and C#

In Java I have written string for the replacement in regex like that:
$123
and expecting result would be:
some_captured_string23

In .NET it is not working.
We have to add brackets around group number:
${1}23

Where is the plus and minus in the speakers

It is so simple that I didn’t know about it😛
We have to take an AA battery (1.5V) and put it on speaker contacts.
•    If we put it from the plus side of speaker the membrane will lean forward.
•    If we put it from the minus side of speaker the membrane will lean backward.