Thursday, December 15, 2005

Christmas Calendar 11/24

This one goes out to all non-americans

As we all know, dates should be represented on the format
dd.mm.yyyy - or derivatives thereof.

NOT

mm.dd.yyyy

so here is the case: Sql Server 2000 will, in its
Enterprise Manager, display all dates on the format
as defined by the regional settings of the local user
looking at the database.

both the Database and the Sql Server has settings for locale and
language etc. but they are all overridden by the locale of the user.
keep this in mind when, oh say, remoting into a server using some
admin user to do stuff on the DB through E.M.

You might just be editing dates the "wrong" way

Monday, December 12, 2005

Christmas Calendar 12/24

In javascript the "enabled" property is instead called the "disabled" property with opposite effect. How curius! Does this point at some underlying mentality on the part of the JS developers?

Saturday, December 10, 2005

Christmas Calendar 10/24

Array casting.

I have stumped on this one in the past:
Why can't I cast an array of one type to an array of another type?.

I fill my ArrayList with nothing but strings, I need to pass the content as a simple string-array (maybe in conjuncture with some web-service).

I call the ToArray(typeof(string)) on the arrayList and we should be done....

.. almost anyway, I also have to typecast the Array returned into the string[]

string[] stringArray = (string[])stringList.ToArray(typeof(string));

ok, this works.. but what if my strings are already contained in a object array, such as would be returned from a call to the simple ToArray() function on ArrayList.

would this work?

object[] object_array = stringList.ToArray();
string[] string_array = (string[]) object_array;

sounds simple? well it is, .. it is too simple, so simple, in fact, that it won't work. You can't bulk-cast an array and all of its content in one go.

(but wait, didn't we just do that above? .... no we didn't. The content was already cast into the correct form. That's how come even though we supplied the object type we still only got a generic Array back the generic Arrays content was correct. only the shell was needed to be explicitly cast into the correct form)

so. what to do eh?
do we have to settle for the old an known?

for(int i = 0; i<object_array.Length; i++)
{
string[i] = (string)object[i];
}


not bloody likely! Let's instead use the Array.CopyTo() function!

string[] string_array = new string[object_array.Length];
object_array.CopyTo(string_array, 0);

so there's two ways of array-cast
Merry xmas to everyone :)

Friday, December 09, 2005

Christmas Calendar 9/24

Why don't my 3.rd party DLL come along to play on my deployment server when I choose "Copy Project" on my web solution?

If your web solution is layered into different projects with a separate DLL for, say, business logic. If your BL project needs DLLs, you need to put them into the web project Lib folder, add them to the project and reference them from the web-project. This way they will play along nicely!

Thursday, December 08, 2005

Christmas Calendar 8/24

What to do when you want to bind more than one field into a dropdownlist?

You've created your object,

BEHOLD MY BEAUTIFUL OBJECT "MyObject" !!!

and you want a Dropdown to contain, say, both the Id property and the Name property.

"DOH" you might say, now I've got to create a new property that contains the textual representation of the Id and Name in the object.. "why oh why must my beautiful object depend on the whims of my presentation layer"

fear not ye brave coders. the solution is nigh!

write one of these beauts and you'll be set.

DropDownList1.DataTextField = MyObject.Id & ”-” & MyObject.Name;
DropDownList1.DataValueField = MyObject.Id;
DropDownList1.DataBind();

The & is not to be confused with && as in "logical AND". but is more like "AND concatenate THIS!! "

Wednesday, December 07, 2005

Christmas Calendar 7/24

Say you need to get the last version before some point in time of a record from a DB that stores multiple versions of the same data timestamps, what would be the most efficient way?

After testing a few things we arrived on this little beauty, simple yet effective.

select * from tblMyTable a
WHERE not exists
( select 1 from tblMyTable a1
where a1.version > a.version and a1.id = a.id and a1.version <= @pointInTime )
and a.version <= @pointInTime )


A trap one might blunder into is not narrowing the outer query by @pointInTime which would give you too many records back

Tuesday, December 06, 2005

Happy happy joy joy

A: I would like to congratulate myself on passing on Microsoft certification exam 70-300 (.Net Architect)

A: Thanks Andreas! That's so nice of you! But how did you know?

A: Coz you just won't shut up about it.

A: Aahh. That'd explain it!

Christmas Calendar 6/24

Never ever ever ever ever ever ever ever ever ever ever ever ever ever
declare Enums / constants in the business layer of an application (or any other conventional layer for that sake).

Before you know it you will need to access those from a layer that doesn't have access to the business layer, and then my friend you are screwed.

Put them instead in a util - project that is included into every other project, that way you won't have to use sql like "where ProjectStatusId = 23" in your dataaccess-code.

The same goes for accessing application configuration settings. Write the code once in a util-project and walk away knowing that it's there.

"but why not just put this stuff into the domain model" - you ask...
1) because Andreas says so
2) because more stuff gets configured through config settings and constants than just domain logic. keep it clean and DRY!.. take it out into a separate (small and cute) util-project

how does the lyrics to that song lyric go?

--"you got to keep 'em separated .. *DÆNH DÆNH DÆNH DÆNH"

Monday, December 05, 2005

Christmas Calendar 5/24

A quick one today:

If you want controls in datagrids to be available in the viewstate, they must be declared and initialised within the page_init. If you want access to calculated values in the footer of a datagrid, you need to make sure the controls containing those values are initialised in page_init or you're screwed.

This is a result of the TrackViewState method on DataGrids being called just before the Load event is fired. The TrackViewState method does exactly what you would expect, it tracks the viewstate, ie. it puts all its controls into the ViewState

Sunday, December 04, 2005

Christmas Calendar 4/24

When working in .Net, you might wonder why it doesn't round off numbers the same way you learned in school.

.Net uses what is called "Bankers rounding" which means that in the event of a tie (e.g. 2,5) it will round towards the nearest even number, so

Math.Round(2.5) --> 2
Math.Round(1.5) --> 2

This is due to the fact that consistently rounding up (as you might remember from your schooldays) will result in a strictly increasing rounding error for all "tied" numbers (.5). Since banks typically have quite a few transactions each day, the term "strictly increasing rounding error" sounds bad to them.

why does .Net provide this rounding as default? -dont know dont care

what do we do about it? - do know do care:
define and use MidpointRounding.AwayFromZero as parameter in a custom util method.

public enum MidpointRounding
{
ToEven, AwayFromZero
}

public class Util
{
public static double Round( double number, int digits, MidpointRounding roundingType)
{
if (roundingType == MidpointRounding.AwayFromZero)
{
double multiplyValue = Math.Pow( 10, digits );
double dRound = Math.Floor(((number*2*multiplyValue)+1)/2)/multiplyValue;
return dRound;
}
else
{
return Math.Round(number,digits);
}
}
}

Saturday, December 03, 2005

Christmas Calendar 3/24

Modulus operator (%)

It might be obvious to some, especially those who hva studied maths (and remember the curriculum) that the modulus operator can return negative values.

The modulus operator will return an answer within 0 +- n

For instance: 13 % 10 = 3
but -13 % 10 = -3

This is different from for example ruby's implementation that will return 7 on that same query. try it yourself.

if you want (imho) logical modulus, write your own util-method:
public static int Mod(int i, int n){
return (((i % n) + n) % n)
}

Thanks to Erlend W. Oftedal for help on this post

Friday, December 02, 2005

Christmas Calendar 2/24

Iterating enums

it's not trivial to iterate enums in .Net 1.1

A solution is to do the following (iterating MyEnum):
foreach(string enumstring in Enum.GetNames(typeof(MyEnum)))
{
MyEnum enumItem = Enum.Parse(typeof(MyEnum),enumstring);
...
}

This will only work if the enum has a different GetHashCode() for all the
items in it. Otherwise only the first item with a given hashcode will be used (multiple times).

This is due to the ridicoulus fact that Enum.Parse does not match the name given with the enum's ToString (as one would expect) but rather matches on the hashcodes...

go figure!

Thursday, December 01, 2005

Christmas calendar 1/24

Today I will talk about something I've been stumped on a few times. It's sooo easy, but it's also easy to forget, so here goes:

DateTime Accurracy

A) The .Net DateTime type holds its accurracyin ticks: each tick is 100 nanoseconds.
IE. there are 10000 ticks each millisecond.

B) The Sql DateTime type only holds accurracy down to three milliseconds.

C) The .Net DateTime Type does *not* output its milliseconds in the standard ToString()



These three points work in unison to make it hard to work with datetimes in .net. One could always just use SqlDateTime, but this datatype is hopelessly useless (no functionality).
Frequently I find I wish we just stored the ticks in the database and did the conversion manually in my current project.

Tuesday, November 29, 2005

Hello World

This will be my third attempt at actually keeping a blog alive for more than a few months. This time I will try to keep it fairly technical and up to date with problems I encounter and (hopefully) their solution.

Mostly, this blog will contain .Net stuff as this is the world I live in at the moment, but I hope to cram in revelations on TDD and Agile development as I get more into these methodologies in time.

So.. I don't expect people to actually read my blog, but if you read this now, then I was wrong and you were right in coming here. (hopefully)

Anyhoo...
I will try and start off with an exercise in self-discipline and present a different christmas calendar containing 24 of my most recent blunders (and their solutions).

As I don't make any claims of Godhood within the field, please feel free to correct me where I'm wrong or provide your own blunders where appropriate. Just remember to keep the tone light and somewhat entertaining. I will excercise my right to delete posts that are rude, abusive, or [insert bad adjective here] language. But all posts written with good intent will live on in eternity. (no matter how bad light they might put me in) After all, we're all happy amateurs in a field that has no defined boundaries of professionalism.