Monday, April 19, 2004

Javascript: When 44.30 + 22.18 does not equal 66.48... 

Well, there is always something new to learn. Some wasted time with doing Javascript has taught me a lot. Before starting check out here for a very good Javascript FAQ. Anything you think is wrong with language check there ( comp.lang.javascript FAQ ).

Now to my problem. I was joustling with issue where the two figures were not adding to what they should. Somewhat like that Title of the post. It was adding up to 66.479999999. I was like totally perplexed with alternately cursing Microsoft (was checking my code in Internet Explorer :) ) for buggy javascript implementation and scrouging the web with google.

Cutting it short the reason lies with the way the Javascript numbers are represented. It is something called IEEE-754 Doubles, which has a resolution(accuracy) of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to about 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range.

In particular, non-integer results should not normally be compared for equality; and non-integer computed results generally need rounding. (From the site mentioned above.)

Tuesday, April 06, 2004

Windows: A few Gotchas! 

Trial and Error Stuff

1. Have you ever used any of the IndexFromPoint function in ListBoxes, TreeViews and such? Does not seem to give the right answer always. Anyway the reason is the way to get the right index because of difference between screen and client coordinates is something like

Point pt = listEx2.PointToClient(new Point(e.X, e.Y));
int i=listEx2.IndexFromPoint(pt.X , pt.Y);

You can of course combine this in one line. Just for clarity.

2. Have you ever used COM Interop and gotten the error "Referenced assembly 'Interop.xxxxxx' does not have a strong name". The easiest way to overcome is to follow these steps.
1. Open the properties of the Project
2. In Common Properties, go to General
3. In the Wrapper Assembly Key File field, add the key file.

In order to know what is key and how to generate a key file see the previous posts.

Thursday, April 01, 2004

Internet Explorer: Cancelling the Refresh 

I think quite a lot of developers would have come across the problem of somehow cancelling the refresh action of the user. Normally on a posted page, a box with re-posting message shows up which allows you to , well, re-post or cancel. From my searching till now, there is no way you can stop the refresh. If you think you do not have the ID10t user problem, below is the suggestion that I found at various places. User onBeforeUnload event in the page.

<BODY onBeforeUnload="event.returnValue = 'Press Cancel to not Refresh.';">
But the problem is this event is called in the following circumstances.

1 Close the current browser window.
2 Click the Back, Forward, Refresh, or Home button.
3 Click on an anchor that refers the browser to another Web page.
4 Invoke the anchor click method.
5 Invoke the document write method.
6 Invoke the document open method.
7 Invoke the document close method.
8 Invoke the window close method.
9 Invoke the window open method, providing the possible value _self for the window name.
10 Invoke the window navigate or NavigateAndFind method.
11 Invoke the location replace method.
12 Invoke the location reload method.
13 Specify a new value for the location href property.
14 Navigate to another location by entering a new address or selecting a Favorite.

Of course one can do solution on the server to handle reposting by session variable and cookies manipulation depending on the situation. But seems like no simple solution.

.Net: Playing with the GAC ....continued 

In order to faciliate my work I had to create a Custom Windows Control and since I was trying to use it in project which had a strong name, this particulat control also needed to be strong named. So, following are the steps to do strong-naming the assembly.

1. First thing one needs to do to create a strong name assembly is to generate strong name keys for the assembly. Microsoft Visual Studio ( not .Net Framework) ships with a Strong naming tool (sn.exe) which you can find at %Program Files&\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin. Usage of the tool is like

sn.exe -k .snk

snk=strong name key
Copy this file to the folder in the assembly which you need to strong name.

2. In the AssemblyInfo.cs file of the assembly you are trying to strong-name you will need to add the location of the keys like this
[assembly: AssemblyKeyFile(@"..\..\..\.snk")]

if the snk file lies in the folder above the source code folder and assembly file(dll) in the bin/debug.

3. In order to add the assembly to the GAC, use gacutil \i

Also read the previous post for pitfalls.

That is it, your asembly is strong-named and in the GAC!!

This page is powered by Blogger. Isn't yours?