ILearnable .Net

December 15, 2005

Christmas Calendar 11/24

Filed under: Uncategorized — andreakn @ 00:22

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

December 12, 2005

Christmas Calendar 12/24

Filed under: Uncategorized — andreakn @ 00:54

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?

December 10, 2005

Christmas Calendar 10/24

Filed under: Uncategorized — andreakn @ 04:54

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
{
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 🙂

December 9, 2005

Christmas Calendar 9/24

Filed under: Uncategorized — andreakn @ 01:45

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!

December 8, 2005

Christmas Calendar 8/24

Filed under: Uncategorized — andreakn @ 00:13

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!! “

December 7, 2005

Christmas Calendar 7/24

Filed under: Uncategorized — andreakn @ 01:29

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

December 6, 2005

Happy happy joy joy

Filed under: Uncategorized — andreakn @ 06:44

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

Filed under: Uncategorized — andreakn @ 06:38

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”

December 5, 2005

Christmas Calendar 5/24

Filed under: Uncategorized — andreakn @ 06:03

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

December 4, 2005

Christmas Calendar 4/24

Filed under: Uncategorized — andreakn @ 05:54

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);
}
}
}

Next Page »

Create a free website or blog at WordPress.com.