2008.04.17

C# API documentation issues

Kategori: Brok, Programmering — Michael Schøler, Kl. 09:07:18

According to http://msdn2.microsoft.com/en-us/library/system.datetime_methods(VS.71).aspx the AddHours() method for any DateTime instance has the following effect:

AddHours: Adds the specified number of hours to the value of this instance.

This means that given a DateTime object “dt”, and that the method AddHours is called on that object, the specific object instance “dt” will be updated with the new value.

DateTime codecompletion hint

As can be seen above, this is also what is shown in the codecompletion hint text in Microsoft Visual Studio 2005.

So the following code should yield the new date value “2008-04-17 10:03:00″:

DateTime dt = new DateTime(2008, 04, 17);
// The object instance "dt" is now equal to "2008-04-17 00:00:00
dt.AddHours(10.0);
dt.AddMinutes(3.0);
// "dt" is now "2008-04-17 00.00.00" ?!

… but instead it yields the value “2008-04-17 00:00:00″.

Digging further in the API documentation you’ll find a contradiction on the method description pages, eg. http://msdn2.microsoft.com/en-us/library/system.datetime.addhours(VS.71).aspx:

Return Value
A DateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value.

This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

So the correct code is:

DateTime dt = new DateTime(2008, 04, 17);
// The object instance "dt" is now equal to "2008-04-17 00:00:00
dt = dt.AddHours(10.0);
dt = dt.AddMinutes(3.0);
// "dt" is now "2008-04-17 10.03.00"

And that’s not playing nice - now is it? The definition text is ambiguous. Looking closely to the hint text you’ll notice that a DateTime object instance is returned when calling the AddHours method: “DateTime DateTime.AddHours(double value)”.

I suggest correcting the documentation text to the following unambiguous definition:

AddHours: Returns a new DateTime with the specified number of hours added to the value of this instance.

2008.02.21

Coding proverbs

Kategori: Programmering, Sjov og ballade — Michael Schøler, Kl. 11:14:59

Nerd alert. You have been warned.

if (!kill(this)) { this.strength++; } // Thanks to Rasmus for starting this

var bear = { sell: function() { if (!this.shot) return false; } }

var volume = (barrel.contents.count() == 0 ? 100 : 50);

if (house.opacity < 1) { throw new Exception(”You should not have done that”); }

if (water.style.display!=”none”) { water.maxTemperatureDegC = 99; }

if (you != null || you == null) { prompt(”?”); }

update Hero set [Enabled]=0 where FoodCount = 0 or WaterCount = 0;

while (this.absent) this.heart.fond++;

Continue the madness here: ordsprog.sagdejeg.dk

Keep them pouring in! =)

2008.02.17

Så skal der laves Team Fortress 2 baner!

Kategori: Programmering — Michael Schøler, Kl. 17:21:39

Hammer editor til Team Fortress 2 ... Jeg er nu gået igang med at sætte mig ind i level design til Team Fortress 2. Valve har været så flinke allerede i november at frigive et SDK til spillet, men jeg har desværre først opdaget det nu.

Det skal nok blive sjovt at lege med! =)

2008.01.29

Trimming values from JSON objects

Kategori: JSON, Programmering — Michael Schøler, Kl. 22:37:09

My current work is really focused on Ajax and JSON, and I get a lot of different things done that might be of use to others.

Today I’ve come up with a neat little function for trimming a JSON object given an array of all values to trim out. The trim function recursively runs though the JSON object and deletes all occurences of the specified values. Optionally, any present or resulting empty objects within the provided JSON object can also be deleted in the process.

My function has this form:

function jsonTrim( json, trimValuesAry, bDeleteEmptyObjects )

The first parameter json is the JSON object to trim, and also the resulting object.

The second parameter trimValuesAry is an array of values to trim out, e.g. [ "", null ]. Do note that objects are not treated by jsonTrim as values but as begin part of the data-structure. Therefore you can only specify leaf-values in trimValuesAry that can occur within objects as key/value pairs.

Finally bDeleteEmptyObjects is an optional bool parameter, it may be omitted. The default value if omitted is false. If set to true any present or resulting empty objects are also deleted.

Trimming json = { obj: { string: “foo” }, bar: 42, nil: null } with [ "foo", null ] and requesting empty objects to be deleted would result in json = { bar: 42 }:

Download (right-click and choose “Save as…”)
jsonTrim.js version 0.1.
jsonTrim-jsmin.js version 0.1 (JSMinified).
jsonTrim-v0.1.zip version 0.1 (zip archive containing all files incl. test).

Test (demo):
jsonTrim-Test.htm.
jsonTrim-jsmin-Test.htm.

License:
Creative Commons GNU LGPL.

Revision history:
2008-01-29, 22:37:
v0.1 first release.