And then I needed an indexer for my type safe collection

Share

Todays task was to export the type safe collection over a web service boundary. This means that the XmlSerializer needs to be able to run on the collection. Initially I got this error message:

    You must implement a default accessor on
    xxxWebService.Service1+ThingieCollection because it inherits from
    ICollection.
    Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code.
    
    Exception Details: System.InvalidOperationException: You must
    implement a default accessor on
    xxxWebService.Service1+ThingieCollection because it inherits from
    ICollection.
    

The solution was to add an “indexer” to the collection implementation, as so:

public class ThingBase
{
}

public class Thingie : ThingBase
{
	public string m_val;
	public Thingie(string val)
	{
		m_val = val;
	}
}

public class OtherThingie : ThingBase
{
	public int m_val;
	public OtherThingie(int val)
	{
		m_val = val;
	}
}

public class NotAThingie
{
}

public class ThingieCollection : CollectionBase
{
	public void Add(ThingBase a)
	{
		List.Add(a);
	}

	public ThingBase this [int i]
	{
		get
		{
			return (ThingBase) List[i];
		}
		set
		{
			if ( i >= Count )
			{
				Add(value);
			}
			else
			{
				List[i] = value;
			}
		}
	}
}
Share

A little less busy again

Share

So, I’ve been very busy recently becuase of the various things one needs to do for the end of the financial year. This has included stuff for work, marking of university assignments, a report and plan for my PhD, and preparing for several talks that I am giving overseas in the next month or so. It’s nice that things are starting to settle down again, but I think it’s not going to get much quieter until I actually leave to go overseas…

Share

A type safe C# collection

Share

This is just a simple example from me exploring how to build a type safe collection which can store more than one type. It’s really very simple — all you have to do is overload the Add() method, and ensure that only the types that you want to get added to the collection have an Add() method. Everything else will then get a compilation error.

using System;
using System.Collections;

namespace typesafeCollection
{
	/// 
	/// Demo of a type safe collection
	/// 
	class Tester
	{
		[STAThread]
		static void Main(string[] args)
		{
			ThingieCollection myThingies = new ThingieCollection();
			Thingie a = new Thingie("a");
			Thingie b = new Thingie("b");
			OtherThingie c = new OtherThingie(1);
			OtherThingie d = new OtherThingie(2);
			NotAThingie e = new NotAThingie();

			myThingies.Add(a);
			myThingies.Add(b);
			myThingies.Add(c);
			myThingies.Add(d);
			// myThingies.Add(e); Compile error for wrong type

			// Iterate through the thingies printing them out
			foreach(Object thing in myThingies)

			{
				try
				{
					Console.WriteLine("Type is: " + thing.GetType());
					if(thing.GetType().ToString() == "typesafeCollection.Thingie")
					{
						Thingie tempThingie = (Thingie) thing;
						System.Console.WriteLine("String thingie: " +
							tempThingie.m_val);
					}
					else
					{
						OtherThingie tempOtherThingie = (OtherThingie) thing;
						System.Console.WriteLine("Integer thingie: " +
							tempOtherThingie.m_val);
					}
				}
				catch(Exception except)
				{
					Console.WriteLine("Exception: " + except.Message);
				}
			}
		}
	}

	class Thingie
	{
		public string m_val;
		public Thingie(string val)
		{
			m_val = val;
		}
	}

	class OtherThingie
	{
		public int m_val;
		public OtherThingie(int val)
		{
			m_val = val;
		}
	}

	class NotAThingie
	{
	}

	class ThingieCollection : CollectionBase
	{
		public void Add(Thingie a)
		{
			List.Add(a);
		}

		public void Add(OtherThingie a)
		{
			List.Add(a);
		}
	}
}

On further inspection we can simplify this a little by having all the object types we want to store in the collection inherit from a base class. For example:

using System;
using System.Collections;

namespace typesafeCollectionWithInheritance
{
	/// 
	/// Demo of a type safe collection
	/// 
	class Tester
	{
		[STAThread]
		static void Main(string[] args)
		{
			ThingieCollection myThingies = new ThingieCollection();
			Thingie a = new Thingie("a");
			Thingie b = new Thingie("b");
			OtherThingie c = new OtherThingie(1);
			OtherThingie d = new OtherThingie(2);
			NotAThingie e = new NotAThingie();

			myThingies.Add(a);
			myThingies.Add(b);
			myThingies.Add(c);
			myThingies.Add(d);
			// myThingies.Add(e); Compile error for wrong type

			// Iterate through the thingies printing them out
			foreach(Object thing in myThingies)
			{
				try
				{
					Console.WriteLine("Type is: " + thing.GetType());
					if(thing.GetType().ToString() == "typesafeCollection.Thingie")
					{
						Thingie tempThingie = (Thingie) thing;
						System.Console.WriteLine("String thingie: " +
							tempThingie.m_val);
					}
					else
					{
						OtherThingie tempOtherThingie = (OtherThingie) thing;
						System.Console.WriteLine("Integer thingie: " +
							tempOtherThingie.m_val);
					}
				}
				catch(Exception except)
				{
					Console.WriteLine("Exception: " + except.Message);
				}
			}
		}
	}

	class ThingBase
	{
	}

	class Thingie : ThingBase
	{
		public string m_val;
		public Thingie(string val)
		{
			m_val = val;
		}
	}

	class OtherThingie : ThingBase
	{
		public int m_val;
		public OtherThingie(int val)
		{
			m_val = val;
		}
	}

	class NotAThingie
	{
	}

	class ThingieCollection : CollectionBase
	{
		public void Add(ThingBase a)
		{
			List.Add(a);
		}
	}
}
Share

Talk for OLS

Share

So, it’s night time here, and I’m in my office at university working on a talk this Thursday. It’s going to be a dry run of my talk for OLS, but I’m starting to think that I wont be able to have all the content ready by then, so it might also be a best effort kind of affair. I think I’ll give this another hour here, and then head home.

Share

Too much marking

Share

It’s hard to believe, but I’m still marking Comp 3310 assignments. I’m not allowed into the office today because I have a medical certificate covering me until the end of today for my weekend illness, so I’ve spent a bunch of today getting as much marking as possible out of the way. I think I should have them all done by the end of the weekend. Hopefully.

Share

Diffie Hellman key exchange using Microsoft’s CryptoAPI

Share

I mentioned a while ago that I was working on implementing Diffie Hellman key exchange using the Microsoft CryptoAPI. It turns out that despite the mightly strong drugs that the Microsoft hackers who wrote the API were snorting, I managed to prevail…

If you want to know how to do this yourself, I recommend that you start off by trawling the Microsoft MSDN Newsgroups. Unfortunately, I don’t think I can show you my code, what with it being proprietary and all.

The hard bit is, how do you test something like this? Sure, the encrypt routine produces something interesting looking when you’ve exchanged keys, but there’s no gaurantee that it’s not just using the default encryption key or something. I’ll need to think about that bit some more.

(I’ve developed a new way of working at home which is very nice. I leave the work laptop and external hard disk on my desk, and then I can wander around the house with my personal laptop. The personal laptop has wireless and runs at a much higher resolution, which means I get more screen space for hacking. I use remote desktop protocol to get to the work machine so that I am still using the right version of the compiler and everything. It’s quite liberating.)

Share

The A team

Share

One of the delights of pay TV is that occassionally they dig up an old show I used to love. As a kid, I was totally enarmoured by the A-Team. Mr T was just so cool. Anyways, Foxtel is reairing the series on TV1, so I couldn’t resist having the TiVo record one for me.

Now, it turns out that like every other show I’ve watched recently which I liked as a kid, it’s crap. Very crap. It did provide an interesting demonstration of how the video player software I am using works though… Initially I just did a screen dump to get the frame above. Here’s what I got:

Note the blank spot where Mr T should be. That’s because the video player is getting the video card to overlay the video, instead of doing it all at the X Window level.

How about some more A team goodness…






Share

Foxtel digital

Share

Well, Foxtel Digital finally got installed after a six month wait on Thursday last week. The installer was really nice (apparently the delay in install is because they only have 13 people doing installs in Canberra), and the install took about an hour and a half.

The problem is now I have to relearn what my favourite channels are. The Nick Jr channel looks to be a winner though.

Share

Microsoft Security Symposium

Share

Went to the second Microsoft Security Symposium and developer thingie on Friday last week. To be fair, I must say that they’re still the only major vendor who has booked out the national convention center for a day (twice) to tell people that their products are insecure, and how they’re working on it.

It’s a pleasant breeze of corporate honesty. I think Robert Scoble needs to realize that when a company has acted in a manner so hostile to other companies (for example crushing Netscape), it might take some time for people to trust them again.

It would be nice if there was more of a focus on non-web developers at the Microsoft developer events though. Apparently there will be slides online sometime, but I can’t find them on the event page.

That Dr Jesper dude in the photo above was cool. Mainly because he clearly knew his stuff, and still had a sense of humour about securing your network — anyone who calls HTTP the “universal firewall traversal protocol” has to be good.

Share

The win32 API is dead

Share

Jeff Waugh points me at another example of a large institution deciding to move away from Windows towards Linux. This is a growing trend — how many did we hear of these five years ago?

Joel raises a related and quite timely point:

However, there is a less understood phenomenon which is going largely unnoticed: Microsoft’s crown strategic jewel, the Windows API, is lost. The cornerstone of Microsoft’s monopoly power and incredibly profitable Windows and Office franchises, which account for virtually all of Microsoft’s income and covers up a huge array of unprofitable or marginally profitable product lines, the Windows API is no longer of much interest to developers. The goose that lays the golden eggs is not quite dead, but it does have a terminal disease, one that nobody noticed yet.” — Joel

It’s interesting that Joel specifically mentions Raymond Chen, who is the only Microsoft employee with an entry in the Linux kernel CREDITS file:

    N: Raymond Chen
    E: raymondc@microsoft.com
    D: Author of Configure script
    S: 14509 NE 39th Street #1096
    S: Bellevue, Washington 98007
    S: USA
    

The article is a good read, even if it is quite long. Joel discusses how he thinks Microsoft has lost the API battle by forcing people into a large upgrade that they’re not going to agree to, and that web applications are where it’s at. He’s omitted and interesting point though — Microsoft’s API is further devalued by the open source implementations of .NET (Mono and dotGNU). Now I can implement a “Windows Forms” application which runs as well on Linux as it does with Windows. This is going to make it a lot harder for Microsoft to sell it’s cash cow Windows in the future as well.

In any case, simple operations like opening a file, writing a string, and closing it using the raw Windows API can take a page of code. In Visual Basic similar operations can take three lines.” — Joel

Ahhh, that would be why I feel so uncomfortable with the win32 API. Doing that with POSIX takes about three or four lines of code as well. Another example is memory mapping a file, which with POSIX takes about three lines, and with Windows takes about three pages and a day of research.

Share