Jul
24th | 2008

Visual Studio Item Templates

Posted by Shahar Y |
Filed under .Net, C#, Visual Studio |

Sometimes we are using the same patterns of code over and over again. Those of us who are lazy (but smart) will create their own code snippets, if you are not familiar with this subject, read about how to create code snippets easily. But what if we need to create lots of classes with the same pattern? Code snippets may not be enough because they lack of some functionality which is needed to achieve our goal. Consider the following code:

abstract class FourWheelsVehicle : IVehicle
{
    abstract public double MaxVelocity { get; }

    abstract public string Manufacturer { get; }

    abstract public double Price { get; }

    abstract public int YearManufactured { get; }

    virtual public int NumberOfWheels
    {
        get { return 4; }
    }
}

An abstract class called FourWheelsVehicle implements the IVehicle interface (which is not presented here because it is not important for our matter). Our task is to create classes which represent each and every existing 4 wheels vehicle, there is a lot of work to do, a huge amount of work. So, I started by implementing Mazda6 class:


Continue Reading...
Jul
21st | 2008

A Programming Job Interview Challenge #13 - Brackets

Posted by Shahar Y |
Filed under .Net, Unmanaged Code |

The thirteen post of the series of programming job interview challenge is out, Only 13 comments with answers were provided to job interview challenge #12. This is a small amount comparing to the previous challenges, but I realize and understand that it was language specific and not very trivial challenge…

Jason Kikel was the first one to solve the question, and here is his short answer:

UnmanagedClass is referencing an address on ManagedClass without pinning it. The ManagedClass instance needs to be pinned so the GC won’t move it to another location during a collection.


Continue Reading...
Jul
16th | 2008

Coding Conventions - Prefix For Class Members

Posted by Shahar Y |
Filed under .Net, C#, Unmanaged Code |

A week or two ago, I read an interesting article in Coding Horror called The Problem With Code Folding. Let me quote the beginning of this post:

When you join a team, it’s important to bend your preferences a little to accommodate the generally accepted coding practices of that team. Not everyone has to agree on every miniscule detail of the code, of course, but it’s a good idea to discuss it with your team and decide on overall approaches and philosophy beforehand. It promotes team harmony, and more than that, it’s just common courtesy. As they say, when in Rome, do as the Romans do. 

Jeff Atwood is talking about the fact that it is important to obey the coding conventions and practices of your team. Every one of us as its own preferences but as a team we need to have some rules, we all should work with the team, not against it. I must admit that I totally agree with Jeff’s attitude, no questions about it.

But, in addition to what was said before, Jeff continues:

Still, there are some coding preferences people may feel.. strongly.. about. If that’s the case, try to clear the air and address those strong preferences up front, as early as possible. Don’t let them simmer. For me, the use of #region is one of those things. I tried to make myself clear in this twitter message: No, I will not use #regions. And no, I DO NOT NEGOTIATE WITH TERRORISTS. Shut up.

I am not going to talk about the #regions issue, but to have my own say about another coding preference:


Continue Reading...
Jul
14th | 2008

A Programming Job Interview Challenge #12 - Managed and Unmanaged

Posted by Shahar Y |
Filed under .Net, C#, Misc., Unmanaged Code |

The twelfth post of the series of programming job interview challenge is out, 28 readers provided answers to job interview challenge #11. I have to admit that I probably failed explaining what I was looking for in challenge #11, because I asked you to provide the best algorithm in both manners: performance and memory. What I really meant is that performance is most important but don’t neglect the memory issue. Due to my little “embarrassing failure”, there are two groups of correct answers - the performance oriented and the memory oriented.

The correct answer which I was looking for (best at performance) as Alex, the first one to provide a detailed solution (its two times in a row), wrote:


Continue Reading...
Jul
8th | 2008

Call Virtual Functions From Constructors?

Posted by Shahar Y |
Filed under .Net, C# |

This one could be easily become one of the Job Interview Questions we publish here at Dev102.com, but I decided to write a “regular” post about this issue because it is an important concept and not a just a puzzle or a brain teaser. Take a look at the following code, can you tell what will the output be?

public class BaseType
 {
     public BaseType()
     {
         Console.WriteLine(“Call base ctor.”);
         DoSomething();
     }

     public virtual void DoSomething()
     {
         Console.WriteLine(“Base DoSomething”);
     }
 }

 public class DerivedType : BaseType
 {
     public DerivedType()
     {
         Console.WriteLine(“Call derived ctor.”);
     }

     public override void DoSomething()
     {
         Console.WriteLine(“Derived DoSomething”);
     }
 }

 public class MainClass
 {
     public static void Main()
     {
         DerivedType derived = new DerivedType();
         Console.ReadLine();
     }
 }

The output of this program is:


Continue Reading...
Jun
24th | 2008

How Do You Exit Your .Net Application?

Posted by Shahar Y |
Filed under .Net, C# |

image

The question in the title does not refer to the actions you take when you are about to exit your application, issues like logging, closing file handles, freeing unmanaged resources and so on. It does literally refer to the exit action itself:

  • Do you use Application.Exit or Environment.Exit?
  • Where do you call the exit method?

The idea for that post came to my mind after I had to solve a weird bug where pressing the Exit button led to a freeze in an application that I work on. It turned out that I was not using the proper Exit method although I was aware to the fact that there are 2 possibilities. Sometimes, when you are in a middle of a coding momentum, writing a lot of code, you can miss the little details. Those will come back to hunt you later on,  and the most difficult thing about it is that everything was functioning fine for about a year before the bug showed his ugly face.

Lets first understand the difference between the 2 Exit methods:


Continue Reading...
Jun
11th | 2008

A Simple ASP.NET MVC Sample Application

Posted by Shahar A |
Filed under ASP.Net, Web Development |

This is a simple hello world example with ASP.NET MVC, to help you build your first application step by step. I will not explain the ASP.NET MVC here, you can find plenty of excellent resources on the web for that, you can try one of these: Kigg - Building a Digg Clone with ASP.NET MVC, ASP.NET MVC Framework, An Architectural View of the ASP.NET MVC Framework . We’ll create a web application with two additional views - the first will ask for your name, and when you submit it you’ll get a greeting message in the 2nd view. Lets start:

1. Download and install ASP.NET MVC Preview 3.

2. Create a new ASP.NET MVC Web Application, Call it MVCHelloWorld

MVCNewProj


Continue Reading...
May
15th | 2008

How to Convert List(T1) to List(T2)

Posted by Shahar A |
Filed under .Net, C# |

Did you ever need to convert List(T1) to List(T2)? One example might be when implementing an interface. you might need to expose a collection of other interfaces (or maybe the same interface), But you usually hold the concrete type implementing the interface in the collection. Lets look at the following example:


Continue Reading...
May
13th | 2008

Beware of List.Find()

Posted by Shahar A |
Filed under .Net, C# |

List<T>.Find() returns the first element found that matches a given criteria. So lets say we have List<int> and we use the Find method on it. What will be the returned value? If a number in the list matches the criteria it will be returned, but what if not? what does the variable zero contains after executing this code:

List<int> listOfInts = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9});
int zero = listOfInts.Find(
    delegate(int i)
    {
        return i == 0;
    });

It has the value of…


Continue Reading...
May
1st | 2008

Operator~ and BinarySearch

Posted by Shahar Y |
Filed under .Net, C# |

Usually, we use Array.BinarySearch to find a value in a sorted array, we all know that this method returns the index of the searched value in the array, if value is found. It turns out that the return value of BinarySearch is much more interesting and useful. Lets focus on what happens if the value is not found in the array.

Those who claim that if value is not found than a negative number will be returned, are absolutely right. But most of us don’t really know the whole truth about that negative number and how it can be used.

magnifying-glass


Continue Reading...

Search Dev102