www.20q.net has to be one of the coolest web sites I have seen in a long time. I could easily kill an hour playing with this thing. I must admit it’s a litte earie how accurate this thing can guess what your thinking about.
“Think of an object and the A.I. will try to figure-out what you are thinking by asking simple questions. The object you think of should be something that most people would know about, but, never a specific person, place or thing.”
Misc
I learned something very interesting today about the framework. Apparently you can compile non-unique method signatures when you are using params. I was building a custom control and creating an override for DataBind() and I wanted to have 2 versions; one that took parameters, and one that didn’t. So I did the natural thing any developer wanting to do this would do and created the method with 2 seemingly unique signatures:
public override void DataBind()
{
LoadControlData();
}
public void DataBind(params object[] args)
{
LoadControlData(args);
}
This code compiles just fine except something very odd happens when you run it. It is impossible to call the empty method signature! If you call the code like this: UserControl.DataBind(), it will execute the signature with params! So if you had some code in the non-param version of the method that was specific to someone calling in without params (such as calling a different loader) it would never run. It makes little sense that passing nothing to a method that defines a single parameter as a params object[] would work (the CLR actually creates a (not-null) zero length array in this case). The odd part is why this code compiles in the first place since the documentation is very clear that params have no bearing on a method’s signature in terms of making it unique. MSDN C# specs state:
Also, note that the return type and the params modifier are not part of a signature, so it is not possible to overload solely based on return type or on the inclusion or exclusion of the params modifier. As such, the declarations of the methods F(int) and F(params string[]) identified above result in a compile-time error.
This is a little confusing since it the docs compare 2 single parameter signatures as indistinguishable, which makes 100% sense since it’s a one-to-one comparison. However, it’s less obvious that method() and method(params object[]) would also be indistinguishable especially since the compiler has no issues with doing this (not even a warning). The problem is when you try and run it, it will never call the empty method and that’s it. You don’t get a runtime error or anything either… you other method just doesn’t run.
It was an interesting learning experience for me today, that’s for sure!
.NET, Programming
C#, method signatures
I will forever be grateful to Ryan for telling me about how to create comma delimited strings out of many-to-many relationships in SQL Server. It’s an absolute gem!
So if were trying to bring back Employees and their territories from Northwind you could do the following:
CREATE FUNCTION dbo.GetCommaList(@EmployeeID int)
RETURNS varchar(500) AS
BEGIN
DECLARE @StringList varchar(500)
SELECT @StringList = COALESCE(@StringList + ‘, ‘, ”) + RTRIM(t.TerritoryDescription)
FROM EmployeeTerritories et
INNER JOIN Employees e ON et.EmployeeID = e.EmployeeID
INNER JOIN Territories t ON et.TerritoryID = t.TerritoryID
WHERE et.EmployeeID = @EmployeeID
IF @StringList IS NULL
SET @StringList = ‘No Territories’
RETURN @StringList
END
And then:
SELECT FirstName, LastName, dbo.GetCommaList(EmployeeID) AS Territories
FROM Employees
This would return the list of employees with all of their corresponding territories in a comma delimited string. Of course there are a number of variations of this same idea that you could so but the prinicple is useful in so many situations.
SQL Server
I had an epiphany this morning on the train as I was thinking about an idea for a useful project that I could do in my free time (about 15 minutes a week) that would not only teach me something new, but be something useful that I could use on a regular basis.
At home I have large number of mp3’s that are hosted on one of the computers I have on the network. This works out great for my wife and I since we can share all our songs and listen to anything we want from any computer in the house (and you can listen to them at work too). Ever since I started listening to mp3’s and realizing how convenient they are I have always imagined a way to somehow get this same collection of music accessible to the main AV system in the house. Getting the computer hooked up to the AV system is a trivial task, but finding a convenient way to select, start, and stop tracks has always been the challenge (the computers aren’t anywhere near the AV system so running back and forth, while it would keep me in shape, wasn’t a good solution). 2 technologies have evolved since I last thought about this idea that has helped solve this problem. The first is wireless networks, and the second is the pocket pc. The wireless networks allow me to control the host pc from anywhere in the house… the pocket pc puts the remote control in the palm of my hand. So I am going to spend the next several months working on a remote controller for my mp3’s that will allow me to pick and play a song, or series of songs on my main AV system. This is actually an idea that is similar to the one Eric Gunnerson is using in his remoting example on MSDN, except I am going to use web services from the PPC instead of sockets (since I already did my time with sockets and ppc.. bleh). I’m more interested in a service approach to this application since it has better support on the ppc and I want to explore more with working with disconnected data on spotty connections.
So the project is set… now all we have to do is find that little thing called free-time.
.NET