Today at work we found a bug. My workmate, not used to C#, usually uses the & operator to compare boolean values. However, in C#, the & operator does not use lazy evaluation.
One curious thing about C# is that it can use two different operators to calculate an and expression: the & operator and the && operator. The difference between both is that the first one (&) can be used both with integer types and boolean types. When used with integer types it will perform a bitwise comparison between the two, and when used with boolean values it will use the logical and operation between the two boolean values, evaluating all the parts of the expression. This means that using a code like this one:
if (someObject != null & someObject.SomeProperty == someValue)
will throw a runtime error if someObject is null, because it will try to obtain the SomeProperty value.
However, the && operator is only available to boolean expressions, and it uses lazy evaluation, this is, if the first condition evaluated is false, it will calculate false without evaluating the rest of the expression, because an and is only true if all the expressions are true.
Conclusion, be sure to always use && when evaluating boolean values if you want to avoid run time surprises
.
October 12th, 2009 | tags:
c#,
programming tips |
No Comments
By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one:

Cropped ComboBox
This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown event as follows:
public class ComboBoxEx : ComboBox
{
public ComboBoxEx()
: base()
{
DropDown += new EventHandler(event_DropDown);
}
void event_DropDown(object sender, EventArgs e)
{
try
{
ComboBox comboBox = (ComboBox)sender; // Catch the combo firing this event
int width = comboBox.Width; // Current width for ComboBox
Graphics g = comboBox.CreateGraphics(); // Get graphics for ComboBox
Font font = comboBox.Font; // Doesn't change original font
//checks if a scrollbar will be displayed.
int vertScrollBarWidth;
if (comboBox.Items.Count > comboBox.MaxDropDownItems)
}
//If yes, then get its width to adjust the size of the drop down list.
vertScrollBarWidth = SystemInformation.VerticalScrollBarWidth;
}
else
{
//Otherwise set to 0
vertScrollBarWidth = 0;
}
//Loop through list items and check size of each items.
//set the width of the drop down list to the width of the largest item.
int newWidth;
foreach (string s in comboBox.Items)
{
if (s != null)
{
newWidth = (int)g.MeasureString(s.Trim(), font).Width + vertScrollBarWidth;
if (width < newWidth)
width = newWidth;
}
}
// Finally, adjust the new width
comboBox.DropDownWidth = width;
}
catch { }
}
}
The following picture shows the results of using the above control instead of the default one:

Non Cropped ComboBox
November 18th, 2008 | tags:
.net,
c#,
programming tips |
1 Comment
This morning I was working on a project at work. It’s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: “are you using a StringBuilder to use those strings?“. And I replied with this answer: “no, I am not“. This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: “don’t tell me this happens with C#, too?“. And he answered: “yes, it does!“.
So, what’s the matter with StringBuilders in C#?
Read the rest of this entry »
One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don’t have standard ways to work with multithreading features, and you usually have to rely on third party libraries to develop thread safe software.
Today I’ll be discussing a design pattern called Double Check, which can be widely used to manage the resource access, elimination and initialization in a safe thread way.
Read the rest of this entry »
Maybe one of the best ways to learn a new programming language is playing with it. Nowadays if you don’t code in Ruby you aren’t cool. I have to recognize that programming in Ruby is funnier than in other language and for the moment I don’t have anything bad to say about it.
Well, I want to introduce Ruby Quiz. It is a collection of minigames prepared to learn Ruby (or to improve your skills). Every week they publish one game, and if you are brave you can send your solution to them and it will be public. I think it’s one of the best ways to learn because you can compare solutions and find were you are weak in Ruby and redo your solution doing it smarter.
While developing a Ruby application or while learning ruby, one of the things you must use is IRB (interactive ruby). As in its man page is said “irb is a tool to execute interactively ruby expressions read from stdin.”. In this tool you can type and execute directly ruby code. It’s very useful but like most other programs like ViM (Vi IMproved) the real power is its customization.
Here I post my .irbrc and to make things clear there are some explanations on each line.
# autocompletion of methods when pressing TAB
require 'irb/completion'
# Wirble is a plugin to colorize your irb, it's installed from a gem (gem install -y wirble)
require 'rubygems'
require 'wirble'
# Make use of readline library
ARGV.concat [ "--readline" ]
# autoindent of code while typing it
IRB.conf[:AUTO_INDENT]=true
# wirble initializations
Wirble.init
Wirble.colorize
As I said before, IRB is very powerful and a proof is that in Ruby Lang they encourage you to try ruby in your browser with an embedded IRB.
Also in rails the console for debugging your application is an irb instance preloaded with all rails configuration. In RailsCasts there is a screencast that shows you some tricks about it.
In the developing process of applications that are not as small as the typical “Hello, World!” examples, there are a variety of factors than can lead to important time savings.
There’s a lot of documentation out there on how to design and specify application before the coding process starts, but there is a crucial factor on success that is not usually spoken of: the way you manage, create and edit your source files.
And of course there are some beautiful software pieces to help developers in that process. They’re called IDEs (Integrated Development Environment).
The problem with most of those IDEs is that they offer so many options that you usually have to read a user’s manual to really take the best from them. Ok, this is something normal, you might say. Maybe you’re right, but be honest, how many software user guides have you read in your life? And I’m not talking about the usual RTFM for a linux man page which can be 4 pages long at most. I’m talking about a user’s manual of 500 pages. I haven’t.
And that’s the reason today I’ll be talking about a nice feature I found on one of the most powerful IDEs out there (regardless of being from Microsoft): Visual Studio 2005 Code Snippets.
Read the rest of this entry »