IT Blog NOW

An Information Technology View by Sam Moreira

Google Docs Now Has Cell Content Validation On Spreadsheets

Wow! This is a feature I would like to have available on Excel, but until Microsoft wakes up, I’ll surely be giving Google Docs a try. Sometimes I have to create forms, in which some cells can only accept a certain type of data. When working on Excel to accomplish this goal, I usually can’t find a native way to validate the data without putting my VBA skills to work. (Note: as I’m not a heavy Excel user, let me know if I’m missing a feature already available in Excel).

Google has added a great feature to Google Spreadsheets, which allows users to determine which cells should match a specific data type. And there’s more; we can also decide whether invalid data will be automatically rejected or if just a warning will be given.

Here’s a quote from Google Blogoscoped about the new feature:

“For instance, you can select a column which is supposed to contain email addresses only. Now pick Tools -> Data Validation from the menu. You’ll be offered to restrict the cell content to only certain types of numbers, texts, or dates. Each type contains further restrictions; for instance, a text may be set so it must contain a certain string, or be an email, or be a valid URL.”

Thank you for this nice feature. Way to go Google!

Free Microsoft E-Learning Courses to Sharpen your IT Skills

If you’re short on money to spend on training or if you’re just looking for a good deal, check out Microsoft Learning. There you can find close to 50 free e-learning courses available on several categories. The majority of them are, of course, introductions and overviews of specific technologies, but there are also a few that could be used to sharpen your skills.

Below you can find a short list with some of the courses that are available for free through Microsoft Learning. For the complete list, click here:

  • Clinic 3402: ASP.NET for PHP Developers: Introduction to ASP.NET
  • Collection 5134: Developing Rich Experiences with Microsoft .NET Framework 3.0 and Visual Studio 2005
  • Clinic 5230: Developing Enhanced Web Experiences with Microsoft ASP.NET AJAX Extensions
  • Clinic 5939: Introducing Server Management in Windows Server 2008
  • Clinic 6189: What’s New in Microsoft SQL Server 2008 for Business Intelligence
  • Clinic 6190: What’s New in Microsoft SQL Server 2008 for Database Development
  • Course 6258: New Features Of Microsoft SQL Server 2008 Reporting Services
  • Collection 6261: Developing Rich Experiences using Microsoft .Net Framework 3.5 & Visual Studio 2008
  • Collection 6262: Introducing Windows Workflow Foundation using .Net Framework & Visual Studio 2008
  • Clinic 6263: Introducing Windows Presentation Foundation using .Net Framework 3.5 & Visual Studio 2008
  • Clinic 6264: Introducing Windows Communication Foundation using .Net Framework 3.5 & Visual Studio 2008
  • Clinic 6335: Exploring Microsoft Application Virtualization
  • Course 6339: Database Fundamentals in Microsoft SQL Server 2008
  • Collection 6340: Introducing Microsoft SQL Server Data Services for Developers
  • Clinic 6341: Overview of Microsoft SQL Server Data Services
  • Clinic 6342: Developing an Application for Microsoft SQL Server Data Services

Happy Learning!

How to Respond and Raise Events in .NET

I was talking to a friend over the weekend and the “Events in .NET” subject came up. Basically, over the years working with .NET, he had created several applications that responded to .NET events, but he had never worked on the other side of the fence: raising the event itself. During the conversation, I’ve realized there’s a possibility that creating classes that raise events is not a common trait among most developers as .NET Framework is extensive enough to address most common issues. So, I’ve decided to create this post to address how to raise and respond to events in .NET.

Let’s start with raising the event on the class side. There are three main steps you need to take when raising an event for a specific situation:

  1. Define a delegate to hold the reference to the method that will respond to the event. The responding method will be specified by the application instantiating the class, not the class itself.
  2. Define an event member associated with your delegate to ensure your delegate will be executed when the event occurs.
  3. Raise the event when the specific situation occurs.

To make things clear, let’s use a simple but real-life example. Let’s suppose you have a Customer class that holds customer records. As using your class is the only way applications can remove customer records from the system, you have been asked to find a way to notify the applications using your class anytime a user tries to delete a customer record, allowing them to validate and log the request before it actually takes place. You can also give applications a way to abort the process if the request is not authorized.

Following the scenario above, let’s apply the three main steps to raise an event.

public class Customer
{
// Step 1. Define a delegate
public delegate void DeletingCustomerEventHandler(object sender, EventArgs e);

// Step 2. Define an event member associated with your delegate
public event DeletingCustomerEventHandler DeletingCustomer;

// Method that raises the event
public bool DeleteCustomer()
{
//Step 3. Raise the event (occurs before the action takes place)
EventArgs e = new EventArgs();
if(this.DeletingCustomer != null)
{
this.DeletingCustomer(this, e);
}

// Continue deleting customer …
}
}

Breaking down the code: In step 1 above, the first parameter of DeletingCustomerEventHandler will be the object raising the event, and for the second parameter, you can either use the default EventArgs class or create your own class based on EventArgs. You’ll usually create your own class if you need to pass any extra information to the method responding to the event. Step 2 defines the DeletingCustomer event, which will be automatically exposed to application developers using your class. And step 3 basically verifies whether or not a method has been assigned to your delegate and raises the event if the delegate is not null.

Now that we have covered how to raise an event, let’s complete the post by talking about the part most developers are familiar with: responding to the event.

In order to respond to an event, application developers must follow two main steps:

  1. Create a method to respond to the raised event; the method must match the delegate signature for the event.
  2. Add an event handler associating your method created in step 1 to the event being raised; this step basically does the magic and allows the event to be raised (see step 3 in the previous section).

Let’s dive into the sample code to respond to the DeletingCustomer event of the Customer class:

// Step 1. Create a method to respond to the event
private void ValidateDeleteRequest(object sender, EventArgs e)
{
// If necessary, cast object back to Customer type
Customer oCust = (Customer)(sender);

// Log Request
LogRequest(oCustomer, oCurrentUser);

// Validate Request
if (!FunctionToValidateDeletion(oCust))
{ // Cancel Operation if allowed by the class }
}

// Step 2. Add event handler when creating the object
Customer oCustomer = new Customer();
oCustomer.DeletingCustomer += new Customer.DeletingCustomerEventHandler(ValidateDeleteRequest);

This code to respond to the event is simple… in step 1 we create a method matching the delegate signature for the event handler. This method will be called anytime a customer record is about to be deleted from the system. So, within the method, application developers can validate and log details of the request for later use. If needed, you can also allow the application to cancel the process when the validation fails by creating a new class based on EventArgs class, in which you would expose a member to trigger the cancellation process.

And in step 2, we ensure that the event will be actually raised by adding the event handler and associating our method with it. If you remember step 3 from the raising an event section above, before raising the event, we must check whether or not a method has been associated with our delegate. The step 2 in this section takes care of this association.

That’s all for now. I hope I have been able to clarify a little bit about how you can raise and respond to events in .NET. As we can see, there’s no mystery in the process; it’s just a matter of understanding how each part fits together to accomplish the goal.

Happy coding!