home

Archive for August, 2008

Inheriting constructors in C#, VB.NET

Wednesday, August 13th, 2008

Several times, I’ve come across the need for inheriting constructors in .NET, and I am constantly annoyed that it does not work. I’m writing a business objects layer, and what I’m trying to do is create some constructors in the base class that can initialize the objects in many different ways, eg:


Public Sub New() ' Create new object using default storage
Public Sub New(ByVal storage as Storage) ' Create new object using specified storage
Public Sub New(ByVal conditions as String, ByVal parameters() as Object) ' Load an object by the specified query, using default storage
Public Sub New(ByVal storage as Storage, ByVal conditions as String, ByVal parameters() as Object) ' Load an object by the specified query
Public Sub New(ByVal id as Object) ' Load an object by the specified primary key, using default storage

And so on. I like to provide many different ways in the base classes, to 1) make writing the objects that inherit from this as easy as possible, and 2) make writing client code as easy and flexible as possible.

Unfortunately, since you cannot inherit constructors (except for the default New() constructor, if that is specified in the parent..), you cannot do this. You have to define stub constructors in each inherited object which call the parent constructor, which ends up being a whole bunch of copying and pasting, and even more work if you ever change/add a constructor.

Eric Gunnerson posted a blog entry about inheriting constructors, and there is a good discussion on it in the comments. I completely agree with Darren Oakey who suggests that constructors should be inherited if and only if no constructors are defined.

This fits in with the default behaviour of inheriting the empty constructor, and generally is compatible with existing code. The only time it would break existing code is if you have an object with no constructors that inherits from another with a non-default constructor, and you specifically want your inherited object to have the default constructor.


Public Class A
Public Sub New(ByVal value as Integer)
End Class

Public Class B
Inherits A
End Class

In the implementation right now, B only has the default (no parameters) constructor. If this change is made, B will have New(ByVal value as Integer), so existing code that assumes it has no parameters will fail. The fix for this is to explicitly define a constructor for B as:


Public Class B
Inherits A

Public Sub New()
End Sub
End Class

One of the arguments against inheriting constructors seem to be that implicit operations are bad and confusing - however, in my view, having the default constructor implicitly added in the above case is exactly the same thing. Additionally, as is pointed out in the comment thread in Eric’s post, this is also how methods behave. Why should constructors be different?

Another option is to add a class attribute that would allow constructors to be inherited, eg:


<InheritConstructors(true)> _
Class B
Inherits A
End Class

It would really make the language more powerful, and allow writing simpler and nicer code. In a simple example:


Dim cust as Customer = Customer.Load("id = {0}", 123)

becomes:


Dim cust as New Customer("id = {0}", 123)

Running network cables

Sunday, August 10th, 2008

With the basement torn out, and access to all of the main floor, I started running wiring for network and video. I’m running two network cables and one cable to each of the bedrooms, two spots in the family room, and one spot in the dining room.

Since the upstairs is finished, I wanted to disrupt it the least amount possible, so I purchased a 54″ flexible installer’s drill bit. It’s 9/16″ which is big enough for all the cables I’m running. The basic process is to cut out the hole in the wall where the wall plate will go, stick the bit in and drill a hole from inside the wall to the floor below, and then pull the wires back up through.

Picture of drill from inside the wall

The bit came with a special tool that goes inside the wall and directs the bit straight down. I had to use this a couple of times when a joist was directly below part of the wall. It also came with a special sleeve that clips onto a hole in the drill bit, grabs the wire, and lets the drill bit act like a fish to pull the wires back up through - very convenient.

I used a low-voltage plastic retrofit box to finish it off. This is easier than a metal retrofit box simply because I’m using keystone jacks and quite a few wires. The retrofit boxes are typically not that big, so trying to terminate several wires, including RG6 coaxial (which is not really that flexible), is easier.

I’m still waiting for all the plates and keystone jacks to arrive, so I haven’t terminated too many of these yet (just a couple to get phones and my media server going). I’ll follow up with a post about the termination later.

Starting to renovate the basement

Thursday, August 7th, 2008

My girlfriend and I just bought a house. It’s a bit older - 32 years old, to be exact - and it shows, in a few places. My first big project is to renovate the basement and turn it into an office / rec-room area, and a laundry room. I figured it would be interesting to some people if I blogged, plus it is some extra motivation for me to get it done in a timely basis.

The basement is not really finished. In the main area, there is wood paneling, acoustic tile ceiling, and painted cement floor. The laundry area is just bare concrete walls, and has all the old cabinets from the original kitchen.

So the first thing I did was rip the ceiling, panels and most of the old cabinets down.

There is a crawlspace under half of the house, and the only paneling I left was for the door to the crawlspace, as I believe the door will fall apart without it. I will eventually figure out something better.

There was some kind of flower wallpaper paneling around the stairs (think wood paneling, but flower wallpaper instead of the faux-wood). This was glued to the drywall, so removing it left chunks of glue, and missing sections of drywall. There’s probably a way to remove it that wouldn’t have done this, but too late now. I just have to decide if it’s easier to sand and plaster, or just put up new drywall..

I also started taking down the wall between the main area and the laundry room, but ran into a bit of an issue with a cold-air return vent. It goes up beside the main beam, runs through the joists over top of the main air duct, and then down into the main cold air return duct. I think I’m just going to try capping it off, and then adding a new cold-air return on the other side of the basement.