Tuesday 27 February 2007

Metric Time

As a computer scientist, working with standard units of time can be a frustrating experience. I'm sure the existing time and calendar systems made sense when everyone followed the gods, the moon, the sun, and the seasons but I think it's safe to say most of the first world population rely on a digital watch and computer-based calendar (and for those who don't, then there's even more reason to move away from their old world equivalents). Since no sane person uses imperial measurement anymore unless they're American or my mom (are either really sane?), I, Pope Michaelus I, hereby deem it 'time' to make the move to metric time. I think we all can handle it.

Everything in glorious units of ten (except weeks—a ten day week might kill me... oh yeah, and months will be a bit weird). So I propose not quite metric time and it all hangs on the seven-day week:

1 year = 10 months

It's pretty simple and time will seem like it goes even faster but you won't get old as quickly.

1 month = 4 weeks

We definitely need to keep months so we can continue having cake days at work once a month. I guess they're communal birthday celebration days really but it's not like I know half the birthday boys and girls anyway. Anyway, cake day might get lost in a year without months. 4 weeks (or 28 days) is a nice compromise and even though 28 is a weird number, at least it will be static. And no leap years—time can move independent of the sun so let it. A month could, following debate, be drawn out to 5 weeks for quasi-metric support but you know what those 31-day months are like—imagine every month being 35 days long. 28 days = more cake.

1 week = 7 days

I'm working a lot of overtime at work these days—mainly weekends—and from current hands-on experience I can tell you do I don't want a 10 day work week. Without a doubt, the business analysts and project managers would jump at the idea so this one's gotta be a special case and it's got to be set in stone up front. 7 is also a lucky number.

1 day = 10 hours

This is where things start getting a bit difficult. A ten hour 'day' is miles off our current 24-hour day so there'll definitely be some adjusting to do. If you put this in context with a week and weeks in months and months in years, those units also start going a bit wonky. But fingers crossed, it'll all work out something like Mexico with lots of sunshine and siesta time. And burritos.

1 hour = 100 minutes

I'm tempted to stick with 10s on this one but seeing as how we've already shrunk down days and stuff... 100 minutes per hour makes sense anyway. Sort of like those 90-minute classes they made me get through in high school—bearable, just. I think all the clock watchers at work would blow a fuse if an hour went by in ten minutes. At any rate, 100s are still metric, so stop your complaining.

1 minute = 100 seconds

No one cares about seconds anyway so just K.I.S.S.

1 second = 1 second

No more of this milli and micro b.s. Like I said, K.I.S.S. and I can never remember which is which so just forget it. Just talk about half a second or tenths of a second or .0484822999299000123 and you'll be fine. That's the beauty of the metric system. Could even just get rid of seconds altogether and just think about bits of a minute—easy!

And of course, most importantly, there will be no daylight savings. I actually read a post out from some guy who manually hacked the Windows registry to program Western Australia Daylight Savings into his PC before Microsoft released their dodgy patch that doesn't work with Vista. I'm a west-aussie so I had to dig around, you see. Anyway, geeks being geeks, I think this guy could have put his time and talents to better use. The debate rages over here but simplicity for us programmers pretty much blows all other arguments out of the water.

By the way, metric time started about half an hour ago so someone better find that registry hacker and see if he fix up our Windows clocks to work all metric like. No doubt Outlook will be off for a while but meetings are a waste of time anyway.

Monday 26 February 2007

Basic CSS

1. Styles (classes and selectors) can be declared so they're relative to their physically enclosing block

#container .relativeClass
{
color: red;
}

<div id="container">
<span class="relativeClass">My red paragraph</span>
</div>

If relativeClass is assigned to an element that doesn't have the 'container' id, the style will have no effect.

The .reativeClass name can be safely reused/redefined within different scopes.

Elements can be specified to have a specific style within the scope of an enclosing block as well:

#divContainer p
{
color: red;
}

.classContainer p
{
color: red;
}

2. Styles (classes and selectors) can also be declared so they're relative to another class or selector

.containerClass .nestedClass
{
color: red;
}

#containerId #nestedId
{
color: green;
}

3. Classes and selectors can be constrained to apply only to a specific element

p.elementLimitClass
{
color: red;
}

p#elementLimitId
{
color: green;
}

<p class="elementLimitClass">My elemented limited class paragraph</p>

<p id="elementLimitId">My elemented limited id paragraph</p>

Friday 16 February 2007

Generic Dictionary Wrapper Class Cannot be Restored from ViewState

The .NET 2.0 generic Dictionary class can be serialised to and deserialised from the ViewState of a web application but extending the Dictionary class to derive a named collection grounded in the business domain requires some extra attention. Essentially, the derived class needs to call a specific constructor in the base class when deserialisation occurs and this doesn't happen implicitly (List-based classes seem to work fine without modification). Note: the derived class will serialise happily--deserialisation is the problem).

The System.Collections.Generic.Dictionary class defines a constructor used to restore the Dictionary's internal data during the deserialisation process:

"Initializes a new instance of the Dictionary class with serialized data."

protected Dictionary (
SerializationInfo info,
StreamingContext context
)


Constructors are not inherited in C# so a wrapper class must explicitly define this constructor and pass the data back up the inheritance hierarchy to the base Dictionary class:

[Serializable]
public class CustomCollection : Dictionary
{
protected CustomCollection (
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context

) : base (info, context)
{
// no implementation
}
}

Depending on how you construct your wrapper class, you'll most likely want to define additional constructors as well or the default (empty) constructor at a minimum as it will no longer be injected by the compiler with the deserialisation constructor present.

Without this constructor, I was receiving a corrupted ViewState exception

The state information is invalid for this page and might be corrupted.
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.

.NET Properties – Infinite Loop

What's wrong with this code?

public bool IsModify
{
get
{
return IsModify;
}
set
{
IsModify = value;
}
}

The title of the post might give away the problem. This code will compile happily and any surrounding code will most likely execute without a problem—until the property is accessed. You have to look closely and compare the property name with the variable name but once you do, you'll realise they're identical; the result is get calling get on IsModify and so until a StackOverflowException is thrown—an infinite loop (the same thing occurs with the set).

What I failed to reveal in the code above is the variable name the property should be wrapping (note the casing):

private bool isModify;

Unfortunately, Visual Studio 2005 doesn't issue a warning about this potentially disastrous little bit of code.

What the problem boils down to is a difference in casing between the variable name and the property. My team's coding standards dictate this naming convention but I otherwise might have used _isModify for the local variable to identify it as such and differentiate it from its property name. On a style note, I find the underscores make the code a bit busy and my eye tends to jump to the variable names—which is probably inappropriate. Alternatively, I could give the variable a completely different name from the property name but that severs the implicit connection between the two.

This can result in all sorts of semi-intelligible error messages. Today, after waiting for the server to run its course, I got my old favourite:

Server Application Unavailable

The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

The event log reveals two entries of interest:

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 16/02/2007
Time: 8:02:51 AM
User: N/A
Computer: PHO02078ITDEV
Description:
clr20r3, P1 aspnet_wp.exe, P2 2.0.50727.210, P3 45063b16, P4 app_web_nr0px690, P5 0.0.0.0, P6 45d4e6d7, P7 8, P8 0, P9 system.stackoverflowexception, P10 NIL.

and

Event Type: Error
Event Source: ASP.NET 2.0.50727.0
Event Category: None
Event ID: 1008
Date: 16/02/2007
Time: 8:02:59 AM
User: N/A
Computer: PHO02078ITDEV
Description:
aspnet_wp.exe (PID: 2168) was recycled because it failed to respond to ping message.

Thursday 15 February 2007

Centre Block CSS2

To centre a block item, set the margin-left and margin-right CSS properties to 'auto'; this sets the computed values of both margins equal. IE won't listen so set the text-align property of the containing element to center as well.

<div style="width: 500px; text-align: center;">
<div style="margin-left: auto; margin-right: auto; width: 100px;">My stuff</div>
</div>

Page & User Control Event Execution Order

  1. ASPX: Page_Load
  2. ASCX: Page_Load
  3. ASPX: Page_PreRender
  4. ASCX: Page_PreRender

Scientific Creativity

Does the term 'scientific creativity' satisfactorily define the analyst programmers?

Wednesday 14 February 2007

List.Clone ( )

The List class does not expose any form of Clone method—either directly through the List class itself or indirectly through its parent classes and interfaces.

To work around this, the List class does expose the AddRange ( ) method. To “clone” a list, simply create a new List and invoke AddRange ( ), passing in the list to be copied.

The result is a shallow clone of the existing list.