Patrick Bader's Blog a blog about software development


Negating Booleans in C#

I was recently asked whether there is a short way to negate the value of a boolean variable. Surely you can simply asign the result of the negated variable to itself:

bool a;
a = !a;

So what is the problem here? Absolutely nothing of course. But now think of some common use case, for example in UI code:

MyForm.MyCheckbox.Checked = !MyForm.MyCheckbox.Checked;

Now, can you think of a way in C# to only write the variable name once?

There is indeed a way using compound assignment operators. The following code does the negation:

MyForm.MyCheckbox.Checked ^= true;

Well, I admit that this is not the clearest solution to the problem of negation, but it surely solves it. There is also an advantage or difference between this code and the initial. In the initial code "MyForm.MyCheckbox.Checked" will be evaluated twice and in the latter only once. Eric Lippert already wrote about this in his Blog.
Whether you will be using this obscure syntax or not is up to you. I for myself favour code where the intentions of the author are obvious which clearly is not the case here.

Tagged as: , 1 Comment

it’s about time…

I finally finished my Master's Thesis about GPU based image processing in the context of a multi-touch application. As I wrote in older posts I have built a prototype of an LCD based multi-touch screen. The PlayStation Eye camera is used to track blobs and fiducials using IR light and the images from the cam are processed on the GPU to extract positions, IDs, etc. For the implementation which will possibly be published in a later post, I used OpenCL, which allows programming on various heterogenous hardware. The visualization is done using solely OpenGL 3.2 Core Profile.

The thesis in in german and can be downloaded here. Feel free to read and comment it.


Adjusting screen brightness with shortcuts

A while ago I installed Windows 7 on my Sony laptop. The good thing about it is it runs quite stable and most things just work. The bad thing some of the shortcuts with the blue "Fn" key don't work anymore. Adjusting the volume works perfectly fine, but setting the brightness of the LCD backlight does not.
Since Sony is not going to release any Windows 7 64 bit drivers for my model, I finally decided to write a little programm myself. This took several hours of searching for the right API to use. There are actualy three different ones:

  1. The first API I tried is called the Monitor Configuration of the Win32 API but did not work for my laptop (some kind of I2C transmission error occured)
  2. The Backlight Control Interface using IOCTL was the first approach that worked for me, but getting the current brightness is not supported on newer Windows versions
  3. So I ended up using WMI which is available for .NET and works surprisingly quite well.

Further usage information and a downloadable binary can be found on my Projects page
If you encounter bugs or have any suggestions please do not hesitate and give some feedback.


… but how is it called in English?

English is not my mother tounge as you can surely guess. So I often have to translate some words from German to English and vica verse or look up words I don't know. I had a nice tool called Babylon Translator. It was just a trial version and ceased to work after the trial period. After living without a translation program for some years and having to look up words in online dictionaries I decided to write a translation program myself since I did not want to open my browser everytime I wanted to look up a word.
The result of a few C# programming hours was a little tool called Translator. At the moment it uses the dictionary at to look up words maybe some more dictionaries will follow.
You can find the tool and source code here.

Have fun testing it and don't forget to give some feedback