Savage.org.za

Tall Stories

Monday, 4 May 2009

Inherited C# and VB.Net Objects Initialize Fields in a Different Sequence

If you’ve ever run FxCop and seen rule #CA2214 “Do not call overridable methods in constructors”, here’s a good example of why you should avoid this mistake. Consider the following contrived example code in C#:

Firstly the base class that contains the virtual call in the constructor:

   1: abstract class BaseClass
   2: {
   3:     public BaseClass()
   4:     {
   5:         SomeInitialization();
   6:     }
   7:  
   8:     public abstract void SomeInitialization();
   9: }


Then the class that inherits from BaseClass overrides the SomeInitialization function and accesses a field in the subclass:



   1: class DerivedClass : BaseClass
   2: {
   3:     object foo = new object();
   4:  
   5:     public override void SomeInitialization()
   6:     {
   7:         foo.ToString();//foo has been initialized
   8:     }
   9: }

Creating a new instance of DerivedClass succeeds because



  1. Field “foo” is initialized with “new object()”

  2. The constructor of BaseClass is called which in turn calls

  3. The virtual “SomeInitialization” in DerivedClass.

However in VB.Net what appears to be the same code, will in fact fail:


The base class:



   1: MustInherit Class BaseClass
   2:     Public Sub New()
   3:         SomeInitialization()
   4:     End Sub
   5:  
   6:     Public MustOverride Sub SomeInitialization()
   7: End Class


The derived class:



   1: Class DerivedClass
   2:     Inherits BaseClass
   3:  
   4:     Private foo As New Object()
   5:  
   6:     Public Overrides Sub SomeInitialization()
   7:         foo.ToString() 'This line will fail because x has not been initialized.
   8:     End Sub
   9: End Class

In the Visual Basic code, the sequence is



  1. The constructor of BaseClass is called which in turn calls

  2. The virtual “SomeInitialization” in DerivedClass.

  3. Field “foo” is initialized with “new object()”

But it will never reach step 3 because step 2 fails with a NullReferenceException. Also be careful of naive fixes like the following:



   1: Class NaiveFix
   2:     Inherits BaseClass
   3:  
   4:     Private x As New List(Of String)
   5:  
   6:     Public Overrides Sub SomeInitialization()
   7:         x = New List(Of String)
   8:         x.Add("A Value")
   9:     End Sub
  10: End Class

In this case field “x” is initialized inside the “SomeInitialization” function, an item added to the list, and is then re-initialized once the constructor sequence has completed, resulting in an empty list. This is one of those sneaky language differences to catch the unwary.

Sunday, 25 January 2009

Road Trip : Inverloch

Yesterday I decided to make the trip to Inverloch after checking the forecast (around 20 knots of wind) and reading that a bunch of other people would be also sailing there on the local seabreeze.com.au forum.

Inverloch is about 140km from where I live and I was umming about whether it would be worthwhile, after actually making the trip I can say yes, it’s definitely worth the trip. I launched from the angling club, about 600m to the left of the main pier, there’s some nice grassy areas for rigging, plenty of parking and a bit of shelter from the wind. There are some large sand banks that protect the inlet from the open sea while still allowing the wind through.

When I arrived the wind was blowing about 25+knots due to some small squalls passing over, I stood on the bank and watched the other windsurfers. My smallest sail is a 5.7sqm so that’s way too much wind. The rain soon passed and the wind set in for around 20knots for the afternoon, I was still a bit overpowered but not so much that I couldn’t sail. It was great to sail some flat again and I managed to put 29.5knots on the GPS. After 3 hours of blasting I was too tired to even try gibing properly and made a habit of falling in at the end of every run.

I know I'm going the right way GPS Track (that wierd line is me falling in an gps losing signal) Plenty of windsurfers Plenty of wind

The speed strip at low tide

Monday, 15 December 2008

Lost Fin Freestyle

I went windsurfing at Ricketts Point again yesterday, this time in a SW wind so I was launching from the other side of the point. The wind was somewhere between 25kts to 30kts and chop bigger than any I've sailed up to now (I'm guessing about 1.5m), adding to that the half dozen poles and bouys in the water indicating where the rocks are, made for a pretty scary ride.

After getting the explanation of what the bouys and poles are and where the rocks are, I basically got the idea that there are rocks *everywhere* except between two of the poles. So the first attempted launch ended after about 10m as I discovered the boom too high, the gust too weak and my booties not fitting in the footstraps (I'd been sailing without booties for a bit and had adjusted the straps), turned around and went back to the beach for some tweaking.

The second attempt was a little better as I planed away from the beach, completely missed the poles and bouys area I was aiming for but luckily didn't hit any rocks because it was high tide. Coming back in I wiped off the top of a wave as I nervously tried to weave though the poles and had to waterstart smack in the middle of all the poles and bouys. Things improved after that and soon I was whizzing in and out, making slight rig adjustments.

Somewhere along the line my fin came loose and I lost it somewhere near the beach. The result of that was a 360 degree rotation of the board, followed by a faceplant right near the beach, fin gone, perhaps someone will find it at low tide. I'm pretty glad it happened near the beach, otherwise it could have been an ugly swim back.

Thursday, 11 December 2008

New Windsurf Board

I Finally got some good wind on a weekend to try out the new board
(Tabou Rocket 105) :-)
Went to Ricketts Point (see google earth file attached). The bay had
very little wind when I arrived, then switched on to around 25 knots
in the space of 10 minutes, built up to around 35 knots over the next
3 hours then tapered off. Started off with a 6.7sqm then changed down
to 5.7sqm, was a little too much when it got to 35knots so I took some
photos(the sailor in the pic is not me, that's some Polish dude)
There's a nice grassy rigging area. The first other windsurfer I spoke
to moved from Cape Town in '96, what are the odds? Waves were about
1.5m just beyond the reef, they're like the waves at Sterkfontein as
opposed to having any kind of gap between, still getting a bit chicken
going over these things at speed. The board is awesome, planes easily,
nice and stable, not sure about the pink starfish on it.PB150009 PB150005 PB150010

Thursday, 2 October 2008

My First Windsurf in Melbourne...

...was a little disappointing. I think I expected a little more wind than there actually was so I left the 9.5sqm sail at home. Nicky and Cayley came along so it wasn't complete loss.

Not much happening Cayley's Turn

Air Pressure

ball

We have about 60 plastic balls that look like this in Cayley's toys, that's what happens when you move from Johannesburg (alt. 1753m) to Melbourne (sea level).

Saturday, 13 September 2008

They're here

After two and a half months of waiting, on the 5th of September Nicky and Cayley arrived in Melbourne to start our new lives here in Australia. On the 2nd of September I arrived in Sydney for Tech Ed Australia 2008 so I was several hundred kilometres away when they landed. Great planning...So while I was geeking out in Darling harbour, Nicky and Cayley arrived to empty house. After Tech-ed finished, the company I work for - Readify, had their annual meeting so it was great to meet all the people I work with but have never seen their faces (no offices, mobile workforce etc). I got home the next day and was greeted at the airport by Cayley yelling Daddy and running across the airport to give me a hug and tell me how much she missed me. We had to prise her off me to get in the car seat but she carried on telling me how much me missed me most of the way home. I couldn't have asked for a better welcome.

Another week of work has gone by since then and Nicky has been making do with the few belongings that we have. Our container of furniture arrives sometime next week so that will be a great improvement. We went today to buy a car after Nicky shopped around for the best deal. We ended up buying a Kia Rondo 7 seater, so we'll get that next week too. Here's some pics:

P9130008 P9130009

The sun is out and it's a nice warm day so I'm off to the park with Cayley.