Sunday, October 08, 2006

Discovering Writely

I just discovered the Writely project, which the Google online word processing project.

It is actually very good, providing with most of the features you are looking for in a word processing tool.

It is certainly not as good as a full-blown desktop software but provides some other features like online reviews with other users.

Other cools features are the capabilities to save to other standards formats (Word, Open Office but also PDF !)

And finally it's completely free...

Monday, August 21, 2006

Visual Studio 2003 SP1 finally released!

For those like me who have not yet fully migrated to Visual Studio 2005, MS has finally released the very long-waited service pack for Visual Studio 2003.

The list of bug fixes can be found at http://support.microsoft.com/?kbid=918007

The SP1 fixes a large number of issues, which were bugging us every day. One regret is that it took 3 years for Microsoft to get it out. Let's be positive and say that it will make my last months of work with VS 2003 less painful before I move completely to VS 2005...

Tuesday, August 01, 2006

Int32.Parse() vs Convert.ToInt()

What's the difference between Int32.Parse() and Convert.ToInt()?

That's a good question, isn't it? At least, that's a question that I often hear.

Int32.Parse converts a string variable (or some other valid types) into a variable of type int. However if you pass a non-recognized value (text string, empty string or null string), it will throw an exception (System.NullArgumentException if null else System.FormatException)

Below is the code of Int32.Parse(string) using Reflector

public static int Parse(string s)
{
return int.Parse(s, NumberStyles.Integer, null);
}

It calls internally another signature of the method with all arguments


public static int Parse(string s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo info1 = NumberFormatInfo.GetInstance(provider);
NumberFormatInfo.ValidateParseStyle(style);
return Number.ParseInt32(s, style, info1);
}

Convert.ToInt32 also converts a string (or some other valid types) into a variable of type int. It basically behaves the same way as Int32.Parse() except that it won't throw an exception System.NullArgumentException if a null string is passed to the method, instead it will return 0.

Below is the code of Convert.ToInt32(string) using Reflector

public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value);
}

You may download http://julien.jacobs.free.fr/download/ConvertProject.zip, a windows application, which highlights each case.

You should therefore use the method that better suits your scenario.

Finally for those who are already using .Net 2.0, you may prefer to use the new Int32.TryParse method, which accepts a string, an ouput int and returns true or false if the conversion succeeds. If the conversion succeeds, the output int is set to the result.

Tuesday, July 25, 2006

.Net Events made easy

There is one thing that is always difficult to understand for C# beginners (and even more experience developers): .Net events. Many persons keep asking me questions about events handling.

What is the delegate?
Where/How do I declare my event?
etc...

First reflex is to look for a good article or some documents that will make it crystal clear....but sincerely I was not able to find a good one, or let say a good and not too long one.

What is an event?
An event allows an element (page, control, winform) to send a message that can be caught or not by other elements.

How does it work?
One element defines and exposes an event. This is the sender or the provider. Other elements can then subscribe to this event and do something when they receive it. They are the receivers.

What does it look like in code?
First we will declare the event in the sender (page, control, etc..) To declare an event we will need to define the event, its delegate and the method to raise it. We will also declare a class for passing data along with the event but it is not required.

To declare an event, you will need to define a delegate. A delegate simply defines the definition of the method, which will receive the event (this is the handler)

public delegate void MyEventHandler(object sender, MyEventArgs e);

In the example above, the handler will receive the sender as an object and the arguments of the event of type MyEventArgs (If you don't need to pass arguments through your event just use the default System.EventArgs)

Then you declare your event as shown below (note that the event name "MyEvent" is preceeded with the handler defined previously)

public event MyEventHandler MyEvent;

Finally, declare the method that will raise your event. This is the method you will call to raise the event.

protected void OnMyEvent(MyEventArgs e)
{
if(MyEvent!= null)
{
MyEvent(this, e);
}
}

When you want to raise your event, just write

OnMyEvent( new MyEventArgs(param1, param2) );

Below is the code for the event's arguments class

public class MyEventArgs : EventArgs
{
private string param1= "";
private string param2= "";

public MyEventArgs (string param1, string param2)
{
this.param1= param1;
this.param2= param2;
}

public string Param1
{
get { return param1; }
}

public string Param2
{
get { return param2; }
}

}

Now, let's see how that works on the receiver. We will take a ASP.Net Web Form for this purpose. You will need to declare that the web form should subscribe to the event.
In the OnInit method, you will instantiate the handler passing the method that will handle the event and attach the handler to the event.

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
MyControl.MyEvent += new MyControl.MyEventHandler(MyControl_MyEvent);
base.OnInit(e);
}

The method definition that will handle the event is the one we defined for the delegate. Once you are in the body of the method, you can easily access the parameters sent throught the event arguments.

private void MyControl_MyEvent(object sender, MyControl.MyEventArgs e)
{
string param1 = e.Param1;
string param2 = e.Param2;
//Do your stuff

}

When should I use events?
Events are the basis of programmation in most plaform today. When you click a button, it raises a "onclick" event that you can decide to catch or not.Just think the same way for the blocks you build...

For instance, a block is creating an order; it can raises the event OrderCreated for other blocks to catch it and do whatever is necessary (send a message to a queue, refresh the screen, etc...)
Events are really a powerful way to expose data without being tied to the other blocks that will receive the events.

Well that's it! I hope you find this post useful.

Saturday, April 08, 2006

Need some sample text for testing

I recently discovered a useful function in Office Word that allow you to generate some sample text. It is very useful when you need some text for your tests instead of typing manually "bla bla bla".

Open Word, create a new document and type =rand()
Word will automatically generates the following text:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

You can even pass 2 parameters to the rand() function. First parameter is the number of paragraph to generate and the second one is the number of sentences per paragraph.

By default, rand() is equal to rand(3,5)

Hope this will be helpful.

Wednesday, February 15, 2006

Free virtual CD tool

I finally got my boss to buy me a MSDN subscription :-)

MSDN comes with a lot of ISO file (CD image files), which are great but I did not want to burn a CD or DVD each time.

I looked for a virtual CD tool to mount this image files, but again did not want to pay for it...

I found that Microsoft has released a free virtual CD tool named "Virtual CD-ROM Control Panel v2.0.1.1". However it does not seem to be really marketed and is not available on MS.com
You can find it on Softpedia (http://www.softpedia.com/get/CD-DVD-Tools/Virtual-CD-DVD-Rom/Virtual-CDROM-Control-Panel.shtml)

The tool is rough but does the job!

Installation instructions

  1. Copy VCdRom.sys to your %systemroot%system32\drivers folder.
  2. Execute VCdControlTool.exe
  3. Click "Driver control"
  4. If the "Install Driver" button is available, click it. Navigate to the %systemroot%system32\drivers folder, select VCdRom.sys, and click Open.
  5. Click "Start"
  6. Click OK
  7. Click "Add Drive" to add a drive to the drive list (Ensure that the drive added is not a local drive. If it is, continue to click "Add Drive" until an unused drive letter is available)
  8. Select an unused drive letter from the drive list and click "Mount".
  9. Navigate to the ISO image file, select it, and click "OK" (UNC names don't work).

You may now use the drive letter as if it were a local CDROM device.

When you are finished you may unmount, stop, and remove the driver from memory using the driver control.

Friday, February 03, 2006

IE7 Beta2 Preview available

Microsoft has just released the Beta 2 of IE7.

It can be downloaded from the Microsoft site.

The new UI is simply great and simpler with the useful tab browsing, ripping off all the unuseful menus and toolbars to widen the screen space. A must with the endless growing content of websites. Another great feature to ease content reading is printing improvement, which is supposed to allow you to print pages without them being cut.

I have not tested the supposed now-supported CSS2 standards, but the rendering is good.

The font are rendered slightly differently than with the current browsers. While it is pleasant to read text the font looks like "bolder" (I hope that we won't have all to adjut our style sheets when it is released...)

Monday, January 09, 2006

Add a first blank item in ComboBox

The built-in ComboBox Windows Forms control is often used but it has a drawback in the sense that you cannot add an extra blank item automatically.

This is quite painful as you often like to add an item like "Select one item".

I have created a control BlankItemComboBox, which does this.
It works with DataTable, DataView and Collections. It currently does not support Arrays of objects.

You can download the source code here

NOTE:
There is one drawback which is that you must set the control properties before you bind the datasource.

blankItemComboBox1.BlankText = "Select";
blankItemComboBox1.BlankValue = "0";
blankItemComboBox1.ValueMember = "ID";
blankItemComboBox1.DisplayMember = "Name";
blankItemComboBox1.DataSource = ds.Tables[0];