Archive for February, 2008

Passing Data to Threads and Retrieving Data from Threads

When you do calls to components, which may need some time to respond, it is very essentially to put this calls in a separate thread. Otherwise you risk your application to bug for a while.

When you work with many threads sometimes you will need to pass data between them. Although .NET Framework provides an easy way to achieve this goal, I will show you another solution of this problem, which is more secure. (more…)

Filled under C#. No Comments.

Spiral Walk

We have a square matrix (NxN) of integers. Having given a starting corner point and a direction, we want to walk all the elements from this matrix on a spiral way. I found this problem accidentally and it vexed me. That’s why I decided to create a simple algorithm.

We are going to create a class called SpiralWalk. It provides a method called WalkIt which does all the job. This methods has three arguments: starting row & col index (zero-based) and a direction. Remember, that the point must be a corner point. Every NxN array has four corner points: (0, 0), (0, N-1), (N-1, 0), (N-1, N-1). (more…)

Filled under C#. No Comments.

Numbers Comparison

You have two real numbers and you want to get the greater one. How would you do this? I am going to show you a few variants to solve this problem.

1. Direct comparison

greaterNumber = (a > b) ? a : b;

This method just checks: if a is greater than b then returns a else – returns b.

2. Using arithmetic operators and boolean expressions

greaterNumber = (a > b) * a + (a <= b) * b;

Actually, this is not working in C#, but in C++. In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types.

3. Using Abs method

greaterNumber = (Math.Abs(a-b) + a + b)/2;

I leave this for you to check its truth.

Filled under C#. No Comments.

Arranging Numbers at Random

Let’s say we want to arrange the numbers from 1 to 100 at random. This is a little confusing problem but there is a solution (not only one) for it. The basic way is the following: you have an array where you put your numbers, you generate a random number while this number is not contained in your array, then you add it to the array. Well, this method is very sluggish! When you add numbers to the array you decrease the size of the multitude of possible numbers. And when this size is too little, the while cycle loops very long until you get a random number, which is not already added to the array. Here is this example. I have used a stopwatch to count the necessary time for this method to finish. (more…)

Filled under C#. No Comments.

C# – ?? – Null Coalescing Operator

Very often you have to check different object if they are not null and then perform a kind of action with them. You would do this check like this:

MyClass value = this.getValueBySomeMethod();
Console.WriteLine((value == null) ? "No value returned" : value);

In C# 2.0 there is very useful operator – ?? (knows as null coalescing operator). It checks if the left operand is equal to null returns the right one. If it is not null => it returns it. Here is a simple example.

MyClass value = this.getValueBySomeMethod();
Console.WriteLine(value ?? "No value returned");

Isn’t is awesome? :) You safe writing redundant code and your code looks more pleasant.

Filled under C#. No Comments.