Tuesday, April 21, 2009

And now, what??

Did something like this ever happen to you?

I was working with Visual Studio 2005 and suddenly it stopped responding. I tried to kill the process with the Task Manager after a while and Task Manager stopped responding, too!!!
Everything else in Windows kept working perfectly with the exception of Visual Studio and Task Manager.

Back to the question in the title... Now What?? If everybody knows that to kill a non responding application you use the task manager, what do you do when task manager doesn't respond? To be true, it had never happen to me before.

So, the next logical step was to try to close the task manager....

and after clicking End Now...

Of course it didn't work.

The scary thing here is that the only application I thought unbreakable in Windows is just another mortal application; but then again... it's Windows, I shouldn't get surprised.

By the way, the solution is to restart the PC. Is there anything that a good restart can't fix?

Monday, April 20, 2009

Comparing your objects: IComparable & IComparer Interfaces.

Have you ever try to make your objects “Sortables”? Some WinForms controls have sort functionality when you bind them to a DataTable or other DataSources; one of those is the DataGridView. However, when you bind them to a list of your own business objects, sorting it’s not completely automatic.

That's when Comparing Objects enters to scene.

To sort a list of values (or objects), the control needs to know how to compare them. The interfaces IComparable and IComparer are provided for this goal.


When implementing the IComparable interface, we are saying that a specific instance of the class can be compared to another one.

Let’s say we have an Employee class:

class Employee : IComparable

{

private String employeeId;

private String firstName;

private String lastName;
private Double salary;

}

(You may use properties of the class. For simplicity we’ll use fields)

We can implement the inteface IComparable; the IDE will help us to generate the functions of the interface by putting the cursor in the name of the inteface and clicking “Implement Interface”.


I like the explicitly way, but it’s a matter of taste (or company standards)

class Employee : IComparable<Employee>

{

private String employeeId;

private String firstName;

private String lastName;

private Double salary;


#region IComparable Members

int IComparable.CompareTo(Object obj)
{


}

#endregion

}


Now you just have to decide how you are comparing the Employees. When you say EmployeeA > EmployeeB, how the employees should be compared? Employee A is greatest than B because of his salary? Let’s say that the default comparison will be the employee ID in order to don’t hurt anyone’s feelings.

int IComparable.CompareTo(Object obj)
{

this.employeeId.CompareTo(((Employee)obj).employeeId);

}


We are comparing the employee ID of our object against the employee ID of the object received as parameter in the function.

Here we have two things to point out:
A) We are using the CompareTo method of the String Class; after all, employee ID is a String; and
B) We have to cast the object received as an Employee.

Point A is not an issue. Usually, we are going to have an ID or some identifier property in our class to compare against it. This ID will often be a String, int, DateTime or some sort of base type. The good news is that all of these types implements IComparable, so they have the CompareTo method. But if you need to use a non-IComparable property or need a whole specific algorithm to compare two objects, you can change the CompareTo method to fit your needs. The only thing you have to remember is to return an integer representing if the instance is less, equal or greater than the parameter.

Instance < Object returns a value less than zero
Instance == Object returns zero
Instance > Object returns a value grater than zero

Now, the real problem comes with point B. What if we received an object that is not an Employee? We would have an Exception in that case. So, in order to fix that, we’ll use the Generic version of the IComparable interface that allows only objects of the specified typed.


class Employee : IComparable<Employee>

{

private String employeeId;

private String firstName;

private String lastName;

private Double salary;


#region IComparable Members

int IComparable<Employee>.CompareTo(Employee other)
{

this.employeeId.CompareTo(other.employeeId);

}

#endregion

}


Now we are sure we are comparing two Employees.

This is it for IComparable Inteface. In a (near) future post we'll see the IComparer Interface, the differences between these two and an example of using them.

Thursday, April 16, 2009

Hola Mundo!

Bienvenidos a AlienPeople!

Somos tres desarrolladores que trabajamos para la misma empresa de software en proyectos similares, con diferente experiencia tecnológica. Creemos tener conocimiento para compartir con la comunidad de desarrolladores. Sabemos que tenemos un largo camino y mucho por aprender en nuestras carreras, así que vamos a documentar aquí el conocimiento que ya tenemos y el que obtengamos.

Nuestro Enfoque

Tenemos experiencia en tecnologías como .Net y Java, así que nos enfocaremos más en estos temas. Aun así, hablaremos sobre Patrones de Diseño, mejores prácticas y diseño grafico.

Nuestra Meta

Queremos proveer de tips útiles y prácticos que ayuden a los desarrolladores en sus retos diarios. También queremos compartir algunas herramientas desarrolladas por nosotros, estas estarán disponibles para su descarga gratuita.

Nuestro Lenguaje

Escribiremos este blog en ingles (principalmente), pero nuestro idioma nativo es el español. Tomamos la decisión ya que queremos mejorar nuestras habilidades de escritura en ingles y porque consideramos que todo desarrollador debe de tener un buen nivel de entendimiento con este lenguaje. Por otra parte, sabemos que no hay mucha información en español; es por esto que intentaremos traducir algunos de los posts al español, de esta manera las personas que no hablen ingles se podrán ver beneficiadas con la información aquí contenida.

Así que, aquí estamos. Esperamos que encuentren este blog útil.

Saludos,

Wednesday, April 15, 2009

Hello, World!

Welcome to AlienPeople!

We are three Developers working at the same software company in similar projects and with different technological background. We believe that we have some good knowledge to share with the Development Community. We know there's still a long way in our careers and a lot to learn, so we are going to document here the knowledge we already have and the knowledge we will acquire.

Our Scope

We have background in .Net and Java technologies, so we will focus more on these topics. However, we are going to talk about Design Patterns, Best Practices and Graphics Design, too.

Our Goal

We want to provide useful and practical tips that will help the developers on their daily challenges. But we also want to share some tools developed by ourselves that everybody can download for free and use frequently.

Our Language.

We will write this Blog in English, however our native language is Spanish. We took this decision because we want to improve our skills in English writing and because we consider that any developer should have good understanding of this language. On the other side, we know there is not too much information out there in Spanish; that's why we are going to try to translate some of our posts to Spanish, so non-English speakers can be benefit with our tips and tools.


So here we are. We hope you find this Blog useful.
Thanks for reading!