Sunday, September 19, 2004

Redirecting in the Catch Block

One of my good friend told me that you cannot Redirect in catch without doing something special. Try writing some code that throws an exception and than in the catch block redirect it to some new page. Something like this:

try {
// write some code that creates an exception
}

catch(Exception ex)
{
// Redirect to other page
Response.Redirect("NewPage.aspx");
}

If you try to do this it will throw System.ThreadingException. In order to do this successfully you need to redirect like this:

Response.Redirect("NewPage.aspx",false);

Here false means that the old page thread will not be discarded and will be removed later on by the GC. Another approach can be using an instance of System.Threading and stopping the thread before the Redirection.


Thursday, September 16, 2004

Could not Lock file strange Access Database Error

I was just trying to populate my datagrid with some data from Access Database and I got this error. Took me 1 hour to solve it. Maybe you people will sometime run into this error. Just copy the database file (.mdb file) to the C:/ or any other directory and Access it, offcourse you are going to ask me if this the best solutions well NO ITS NOT. Its actually the worst solution. The problem this error references is about database being opened more users.

I have not found a good solution yet. Offcourse microsoft suggest to play with Registeries, I don't think I would ever like to do that.

Monday, September 06, 2004

WinCV.exe

WinCV is a tool in Microsoft.net through which you can find all the methods, Interfaces, properties that a certain class contains. Its very usefull if you want to find that if this class implements IDisposible Interface or is this method virtual or not.

Just search in your Microsoft Visual Studio.net folder for WinCV.exe and double click to run it.


Saturday, September 04, 2004

C# 2.0


I recently saw Anders Hejlsberg, Lead Architect of the C# language answering questions on the white board.

He talked about many cool features that they are thinking to add in C# 2.0. One of the features I liked was the functionality to reconize class types.

We all write code like this:

Point p = new Point();

In C# 2.0 you can write the same statement something like this:

var p = new Point();

For complete details visit http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20040624csharpah/manifest.xml



Whats wrong in using Static Methods

The question here is that why can't we use static methods in our class instead of making a Singleton pattern. I found the solution and explaination on web. View the code below:

public class A {
public static string staticVariableCopiedFromB = B.staticVariable;
public static string staticVariable = "This is a string defined in A";
}

public class B {
public static string staticVariableCopiedFromA = A.staticVariable;
public static string staticVariable = "This is a string defined in B";
}

public class Test {
static void Main(string[] args)
Console.WriteLine(A.staticVariableCopiedFromB);
Console.WriteLine(B.staticVariableCopiedFromA);
}

Now just try to dry the program. Our common sense suggest that the output of this program should be like this:
This is a string defined in B
This is a string defined in A

Unfortunately this is not the case and you will only see the following output:
This is a string defined in B

Why is that so?
Lets see whats happening in Main().
1) A.staticVariableCopiedFromB: This goes to class A and notices that A.staticVariableCopiedFromB depends upon B.staticVariable, so it goes to B.staticVariable get the string "This is a string defined in B" and assigned to A.staticVariableCopiedFromB.

2) Next is B.staticVariableCopiedFromA: It goes to class B and see that B.staticVariableCopiedFromA depends upon A.staticVariable but then realizes that it was never Initiazed and hence assigns "null" to B.staticVariableCopiedFromA and output nothing.

The problem with static methods is complex Initializations and you never know who can change what and its very hard to find such problems.

Also you cannot override static methods. On the other hand you can Inherit from the Singleton class.


Friday, September 03, 2004

First Blog Entry

Hello Everyone and welcome to my BLOG. Here I will post my thoughts on the .net framework and the languages included in the .net framework.

Thanks everyone,