C#: Structs and parameters

struct MyStruct
{
	public int X { get; set; }
	public int Y { get; set; }

	public MyStruct(int x, int y)
	{
		this.X = x;
		this.Y = y;
	}
}

Will trigger an error on line 8:

The ‘this’ object cannot be used before all of its fields are assigned to

So I thought I might fix it by assigning a value to the fields. Since we have automatic properties, let’s convert them to normal properties:

struct MyStruct
{
	private int _x = 0;
	public int X
	{
		get
		{
			return _x;
		}
		set
		{
			_x = value;
		}
	}
	private int _y = 0;
	public int Y
	{
		get
		{
			return _y;
		}
		set
		{
			_y = value;
		}
	}

	public MyStruct(int x, int y)
	{
		this.X = x;
		this.Y = y;
	}
}

Let’s press F5. Oeh noes, another error:

‘MyStruct._x’ cannot have its instance field initializers in structs

So you can’t do field initialization in a struct (apart from in a function / constructor / getter / setter).

Let’s go back to the first piece of code and make it work by adding 7 characters:

struct MyStruct
{
	public int X { get; set; }
	public int Y { get; set; }

	public MyStruct(int x, int y) : this()
	{
		this.X = x;
		this.Y = y;
	}
}

The this() initializes the struct, and in the constructor itself the struct’s vars & properties are set to their default values. It’s weird though that an int needs initialization…

SQL Server 2008 Express Edition + Visual Studio 2008 on 64-bit: Hotfix released

A while ago I wrote about me having a 64-bit system with SQL Server 2008 Express edition 64-bit and Visual Studio 2008 SP1 not beeing able to create a service based database because of some wrong registry keys. Installing a 32-bit version of the database should have solved the problem but that didn’t work for me. So I reverted to Sql Server 2005 Express Edition.

The problem is described on the Microsoft connect page.

Now Microsoft has released a hotfix for this. To get the hotfix you need to create a support case online (99$, which should be refunded) or use an MSDN support case voucher. Or you can use Google as I did and find the direct link to the hotfix 😉

I AM NOT RESPONSIBLE FOR INSTALLING THIS HOTFIX, IT WORKED FOR ME, BUT IT MIGHT NOT FOR YOU. THERE IS A REASON WHY MICROSOFT DOES NOT MAKES THIS HOTFIX PUBLIC. USE WITH CARE.

C#: Extending Outlook.(_)Application.

Does not work.

Outlook.Application and Outlook._Application are both interfaces. And I want to use them as a class in my program (I don’t know how they made that work).

I wanted to extend Outlook.Application and add a simple method to create a new email. But that does not work since you have to implement a bunch of methods which I hardly understand.

Thank Microsoft for creating extension methods, so here is my solution:

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace SendToExtended
{
	static class OutlookExtensions
	{
		/// 
		/// Creates and returns an Microsoft Outlook MailItem
		/// 
		/// an instantiated Outlook application
		/// A new MailItem object
		public static Outlook.MailItem CreateMailItem(this Outlook.Application outlook)
		{
			if (outlook == null)
				throw new ArgumentException("outlook parameter cannot be null");

			return (Outlook.MailItem)outlook.CreateItem(Outlook.OlItemType.olMailItem);
		}
	}
}