Hi there! Seems you are visiting my site for the first time. Hmm, Why dont you subscribe to RSS or use email subscription service to get all posts at your inbox (No spam). You can also follow me as google friend.
Please carry on reading my blogs, if you like, please give your feedback too. Have a wonderful day! (Dismiss)

Monday, September 7, 2009

Comparision between Response.Redirect, Response.RedirectParmanent and Server.Transfer

It is to be noted, .NET has lately introduced Response.RedirectParmanent() after a long time. The main motive is to have permanent response redirection to the Search Engines.

Response.RedirectParmanent() is an extension function introduced in .NET 4.0.
The main motive of it is to indicate the Response Code to the Search Engine that the page is moved permanently. The Response.Redirect generates Response code as 302 whereas RedirectParmanent returns 301.

Thus say you have a page, and which is included to search engine for a long time, if you use Response.Redirect() it will not change this effect to the search engine(taking this a temporary change), while if you use Response.RedirectParmanent() it will take it as permanent.

If you try to write Response.RedirectParmanent() the code will look like :

public static class Extensions
{
public static void RedirectPermanent(this HttpResponse Response, string absoluteUri)
{
Response.Clear();
Response.Status = "301";
Response.RedirectLocation = absoluteUri;
Response.End();
}
}

In case of Server.Transfer() the actual response is actually been updated. There is no effect to the search engine, and search engine will think the output is coming from the same page that is called upon. Let us give an example :

Say you have 2 pages (Page 1 and Page 2) where Page1 redirects to Page2
In case of

1. Response.Redirect() : Search Engine will take this redirection as Temporary(Status 301) and always keep Page1 in its cache.
2. Response.RedirectParmanent() : Search Engine will take this a permanent redirection(Status 302) and will remove Page1 from its database and include Page2 for better performance on search.
3. Server.Transfer() : Search Engine will be unaware of any redirection been took place (Status 200) and will keep Page1 to its database. It will think Page1 is producing the output response of Page2.

When to use:
Response.Redirect is perfect when your page is temporarily changed and will be changed to original within a short span of time.
Response.RedirectParmanent() when you are thinking of deleting the Page1 totally after the search engines changes its cache.
Server.Transfer() when you are thinking of keeping the page for ever, and to let search engine unaware of this redirection.

Thanks for reading and hope you like the discussion.

Monday, August 31, 2009

New Features of C# 3.0

1. Extension Methods : You can define extension methods to your namespace, so that if this is added, the class in which it is applied will act as instance variable. Example :


pubic void isDateTime(this string x)
{
return DateTime.TryParse(x);
}

The function will be added to the string object, and you can use it normally as with instance methods of String class.

2. Annonymous Types : You can create unnamed objects that is not derived from a class, rather it is created on a fly. Annonymous types comes very handy when working with LINQ.
Example :

var x = new {x=20,y=40};


thus x will have 2 properties x and y.

3. Initialisers : While you create object of a class, ,we now dont need to call its constructors as all the public properties can be initialised easily.
MyClass x = new MyClass {
MyProperty1 = 20,
MyProperty2 ="This is new Property "};

thus we can set properties directly without calling the respective constructors. This feature also comes very handy when we dont want to have lots of constructors overloads or just say we have to create 2 constructors with same set of parameters but which will be assigned to 2 different properties . Accessing properties directly is very handy feature lately.

Also there is collection initialisers like
List mycol = {"ss","gg"};
You can do this without calling add method.


4. Implicitely Typed Variables : C# introduces "var" which means when the object is assigned the type will be determined automatically. if you assing a string to x it will be of string type, or whatever you do.

5. Partial Methods : .NET 3.5 introduces partial methods, where the same methods can be declared more than once so that when called it will automatically add features to the existing method that is already defined.
Note : Partial methods must not return anything, it should be defined as Void.

6. Property implementation is not required : Sometimes we just need to define properties to expose objects. In such case we dont need to implement a property, rather we can go with auto implementation feature introduced.

public string myproperty {get;set;};

The property will implicitly create a private variable and assign the values properly.

7. LINQ and Lambda Expression : You can query .net objects now using Linq. Lambda expressions are short - hand representation of LINQ queries. :)

This is just basics of what introduced in C# 3.0. I will discuss more on each of them when I find time.

Hope you like reading this.

Sunday, May 10, 2009

Javascript pure HTML Dialog Box

It is a very common scenario to make a dimmed effect in web when a javascript popup is opened. Lots of people wonder how those being made. Some people tile with a transparent png to the background while others use div with certain opacity to fill up the entire space. I choice is with divs as they are purely htmls, and you can easily create div dynamically and also remove them from dom.


To do this, First we need a semi – transparent div to cover the entire work area :

var width = document.documentElement.offsetWidth + document.documentElement.scrollLeft;

var height = document.documentElement.offsetHeight + document.documentElement.scrollHeight;

var centerX,centerY;

if(document.documentElement && document.documentElement.clientHeight) // NON IE

{

centerX = document.documentElement.offsetWidth / 2;

centerY = document.documentElement.offsetHeight / 2;

}

else if( document.body ) //IE

{

centerX = document.body.clientWidth / 2;

centerY = document.body.clientHeight / 2;

}

var layer = document.createElement('div');

layer.style.zIndex = 2;

layer.id = 'backgroundlayer';

layer.style.position = 'absolute'; // Required to place layer in background

layer.style.top = '0px';

layer.style.left = '0px';

layer.style.height = document.documentElement.scrollHeight + 'px';

layer.style.width = width + 'px';

layer.style.backgroundColor = 'black'; // Ideal for dim effect

layer.style.opacity = '.6'; // Works only in Non IE browsers

layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=60)"); // IE little fix

document.body.appendChild(layer); // Append to DOM

document.body.style.overflow="hidden"; //To Hide extra scrollbar when dialog is visible.

Explanation :

In the above code we have created a div, set certain properties and append that to body. We need to have DXImageTransform to make opacity of a div in IE as style.opacity cannot work in IE. DX is directX transform, so if you don’t need your application to run over IE, you can remove this line, Otherwise this line will be ignored in Non-IE browsers. documentElement.scrollHeight gives you the actually height of the browser working area adding up the scrollPosition. Background of Div goes perfect with black, but you can change it to any other color according to your choice.

Another little fix for IE is use of document.body to find the center screen position which shows the popup.

Next we add a div in the middle of the page on top of the semi-transparent layer.

var div = document.createElement('div');

div.style.zIndex = 3; // It should be more than background DIV

div.id = 'popupbox';

div.style.position = (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';

div.style.top = '200px';

div.style.left = (width / 2) - 200+ 'px'; // Half its width

div.style.height = '50px';

div.style.width = '400px';

div.style.backgroundColor = 'white';

div.style.border = '2px solid silver';

div.style.padding = '20px';

document.body.appendChild(div);

Explanation :

This is the actual dialog box which is placed over the semi – transparent div. For IE we need to place its position to absolute, otherwise to fixed. (IE always bugs me with non-standard properties, Oh god). Left position is set to its width/2 – 200 as actual width is 200. We also created a solid silver border to the popup.

And finally we put some text and a link that closes the popup when clicked:

var p = document.createElement('p');

p.innerHTML = 'Some text';

div.appendChild(p);

// Create a close link of popup

var a = document.createElement('a');

a.innerHTML = 'Close window';

a.href = 'javascript:void(0)';

a.onclick = function()

{

document.body.removeChild(document.getElementById(backgroundlayer));

document.body.removeChild(document.getElementById('popupbox'));

document.body.style.overflow="auto";

};

So its done, you can try out the live demo if you need as well. Online Demo

Monday, October 13, 2008

Bit on .NET

Hi, To all the programmers who is working in .net should know some of the facts. Well, .Net is not an Operating System. Its also not a general purpose software package. Its just somewhat functionality for the programmers to build their solution in the most constructive and intellegent way ever.
Just to tell you the truth, most of the codes in .Net environment resembles with the JAVA coding as if some people coming from Java would find it as to their native language.
.NET is a Framework that loads into your operating system, which you need not to load in the later virsions of windows like Windows 2003 server or Just a new release of Windows Vista. As it is going to be added as a component in the next generation of windows.
Now, What is the .Net all about??? Well, Its a framework that supports Common Language Runtime (CLR). As the name suggests, Common Language Runtime will be something that will run a native code for all the programming languages provided within the Architecture. Just for example, I would rather say that integer of VB.NET and int of C# are same, as they are both object of the Int32 class. And because of the generation of Intermediate language (Byte code + metadata ) in assembly produced as output during compilation is again compiled by JIT compiler( for those of you came from JAVA must know this). This enables you to bind two assemblies during runtime. Thats what is called basically interlanguage communication.
Now then what is language independence??
Well, to tell you about language Independence, .Net is not at all efficient in that. Just Microsoft built their own version of languages like C++, J# (for Java), C#, VB etc that can communicate between each other. Afterall J#, even if they resembles with JAVA is not purely Java.. Thus, the word may look to you somewhat fake.
Now what is the most prospective part of .NET?
Now, with the growing Internet, ASP.NET may be the most important part of .NET technology.
Well, ASP.NET has a new technology where the controls reside server side. You dont bother to use traditional client side controls. In .NET technology as there is a provision of runtime building of machine codes, a web server can directly compile and transmit codes to the browser during runtime. This is , I think the most approved and widely accepted part of .NET.
NEW things of .NET?
well, during the last two years , Microsoft is constantly changing the .NET technology, that may not be good for a settled programer. Well, first of all, in .NET 2003 Microsoft changed some features and also addes some new things. Well, new things are good, but changing the existing in such a quick succession is not at all good from programmers point of view. Again, in 2005, Microsoft publishes the new release of VISUAL STUDIO.NET 8 . This is a completely new environment. It have new releases of controls, the IDE is also different. Thats not what we all wanted as a programmer. What do you say????
Now, Microsoft is also increasing its scope.. One of the most important feature that is just now introduced is AJAX. Well, the name is what can be said as Asynchronous Java Script with XML.
What is AJAX???
Now, AJAX is not a technology. Its a new technique that can be embeded in your existing ASP.NET application to enable you partially update the web page without totally refresing the page. The introduction of ATLAS controls are also a great achievement. This is the latest trend of the ASP.NET programmers. Just you can see some of the latest updates of yahoo, as in the home page after signing in you can see the mails directly in the home page. Keep a watch. Also the most important findings may be for gmail users. You all dont have to refresh the page for an updation of the page.
In gmail, a chatting environment is also created with the use of ajax, may be...

Now I request to add on comments to my postings so that I can write more on this topic...
Thanks in advance......

.NET Code Construct

Okey... Lets proceed our discussion a bit more.. In my previous topic posted, I have gave you a brief Idea of the whole thing where the technology is facing... Now I will go a bit indepth... and show you how to use the existing technology of .NET to built the most beautiful and fully featured software applications.

To start with VB.NET, I must say, its far far better from the previous version of VB. It is fully Object Oriented Programming Language with all the functionalities of OOPS, so that all the programmers feel better in this language. Also with the full support of .NET Framework, you will have to do only a few codes in this environment.
To work with VB.NET you need the Visual Studio .NET in your machine. Lets start with that one. Go to File->New-Project. Give Location of your application and choose windows Application from the right hand side of the dialog box while choosing the language VB in the left. After you click on open, it should open a new blank form in front of you...

Well, How does everything happened so fast without writing not even a single line of code. Well, Microsoft did it for us already. In case of JAVA you find a Frame class already built in the java.awt package. Here also in case of microsoft's VB, it is just a class already built in by microsoft called Form Class.
Actually, Just similar to JAVA, microsoft has made a hierarchy of classes and stored in a number of namespaces. Namespaces means just a scope that holds classes and other namespaces. The concept is just similar to our so called folder hierarchy... Suppose we think a namespace as a folder and a class a file. So there is drives which we call roots, just similarly here also there is a root namespace called System. Everything that corresponds to system, will be stored within this package. Under this there are other namespaces too.. like Data, Windows etc... Every namespace holds classes or other namespaces. Just similar to the concept that every folder holds other folders or files.
Now coming to our point whenever I open the project, I saw a new blank form in front of me... Its just inherited from a class System.Windows.Forms.Form, so everything defined within the form will come to this form. Well well, thats just a flick of a second, that all the associated properties and functions in the class comes to your place. Isnt it beautiful.
So your form1 is inherited from the already created form with all the listeners of events associated already. Now you can start adding other controls.
Microsoft had created a separate region and made everything within that area... This is Windows Designer Generated Code. Here you will find Sub new() which is nothing but a constructor of your class, so it gets automatically called whenever you run your application, means when you create actual object. The initializecomponent function will initialize the components just before the object comes into work, and all the settings that you have modified with, will be in place properly. Thus whenever you run, everything that you made in this form gets displayed.

Now lets work with some of the controls. In the left hand side you will find a toolbox, where you get all the tools that you require to place in your form. Those are called controls... So whenever you drag any control in your form, a new object of that class is created and it gets displayed over your form.... Is it that much simple... What do you think. ??? Its not.. It looks very simple, but it actually a bit more. I must say, in form class, there is an array called controls array. Now this array is very helpful. In VB.NET everything that you add in this array will be displayed on the form, so within the class form there is something I am sure that draws the entire thing in the form if you have added the same in Controls array.
So whenever you drag one control, say its a button It creates two lines, in general,
Friend withevents button1 as button
Controls.add(button1)

Now withevents keyword is very helpful, in regards to event handling. Whenever we create an object normally, it is not associated with any action listener of the events. Now withevents will automatically add up all the listeners to the control and everything will come under your way....
Otherwise if you create one button without withevents keyword, you need to manually add a listener to the control
Addhandler button1.click, new EventHandler(Addressof button1_click)
that is, we are adding a listener and whenever it occurs, it will call an eventhandler button1_click which is an eventhandler procudure. Otherwise withevents keyword will automatically add up all the events with the button class and call a blank event procedure.

Now lets us look to the solution explorer. You will find a solution explorer, a window, just if you dont see one, go to view and find solution explorer there. After you get it, you will see a hirerchy there also, the first line suggests the name of your solution, means a number of projects can be added up for a single solution. This solution will be written in a file with .SLN extension.
Next, Line is the name of your project. Under this project, the first thing you notice is some references, they are some locations which you can call directly, they are references added automatically, like System.Windows.Forms so to call Form class under this namespace without giving the whole path you can use just Form and get the class name.
After references, there is a file called AssemblyInfo.VB.
Now if you open this file, you will get the metadata associated with your project, mainly class id of your application and many more. The next option is your form, to create a form Microsoft creates two files, one is for code with .VB extension and other is for visual interface with .RESX extension. So if you delete .RESX file from the location, your application looses visual interface. Ok.
The property window helps us to change the default properties of controls, so whenever I select a control, the properties window will change its content dramatically and display all the associated properties associated with that particular control.
Now let us create a button..
In a blank form drag a button, and then double click on the button placed in the form, the code window appears, Write the following

private sub button1_click(Byval sender as System.Object, ByVal e as System.EventArgs)Handles Button1.click
MessageBox.Show("Hello Welcome to the World of VB.NET")
End Sub
[EXPLANATION]
Now if you run your application, and click on the button, it will show a messagebox in front of you.
Now to understand an event procudure, its just like normal procedure, but with some strict rules, Its signature is just similar to what I have shown, otherwise it will not be treated as an event procedure one... So for every event procedure , you must write two arguments, The first one is Object type, now this argument will take the actual object inside your event procedure which have generated the event. So here button1 and sender is the same thing if you are writing within the block.Now what for, just in the very next you have seen handles keyword, its the list of events associated with the event procedure. So now here, one event procedure can handle multiple events. The second argument will take event related information. We will discuss them later on.
Now lets take another textbox in the form and add textbox1.click just after handles button1.click after giving a comma... so that it looks like this

private sub button1_click(Byval sender as System.Object, ByVal e as System.EventArgs)Handles Button1.click,textbox1.click
if typeof(sender) is Button then
MessageBox.Show("Hello You clicked a button")
else if typeof(sender) is TextBox then
MessageBox.Show("Hello You clicked a Textbox")
End Sub

Just in this case you can detect if the procedure is invoked using textbox.click of button1.click. Isnt it look handy.
Now if you look at the messagebox class,there is a shared method show, when called with an argument of string type will print the string in a messagebox...
The return type of show function is DialogResult...
So if I want to trap the result of Messagebox.show we can trap by creating an object of DialogResult
Dim result as DialogResult
result=Messagebox.show("Message","Caption",MessageboxButtons.OKCANCEL)
if result=DialogResult.OK then
Me.Text="OK Clicked"
else
Me.Text="CANCEL Clicked"
end if

Here me represents the Current class, just like this pointer .... of C++ or C#. So the text of the title bar of your form will get changed.

Now Let us create another form, to create a form, right click on the project icon of the solution explorer(Second line of Solution explorer hierarchy) and add->new Item. From the dialog box choose form and click open. A blank for comes in a new tab. Just drag a label here and Write something in its text proerty of properties window.
Nest go to the form1 again, drag another button Let it be button2 and write the code below in the code window

public sub button2_click(ByVal sender as Object, ByVal e as EventArgs) Handles Button2.click
Dim f as new Form2
f.show()
Me.hide()
End Sub

Now whenever you run the application and click on button2, a new form will be displayed in front of you.. So by this way you can navigate from one form to the other. The first line creates a new object for form2 class, next line shows the form, making Form2.Visible=true and Me means Form1.Visible=False. Just to make form2 as startup, you again right click on the project of the solution explorer and go to properties. Change the startup object to form2 and start running.. The form2 will be now your startup form...
Now I think you all can start working with some codes with VB.NET, I will proceed with more discussions in my next posts, so keep watching my posts...
Thanks a lot

Sunday, October 12, 2008

Inheritence and Polymorphism

Well, now I would like to make some more clear ideas about what coding is when you are working in VB.NET.

As I have already told you, the first line just after the class name, you will be getting the inherits part. This inherits part making our task of getting a new form much easier. It would be worthy enough to get a well designed form just with a click of mouse. Isnt it. Its what we all know about the famous feature of any OOPS based language. In case of JAVA we have to derive from Frame class whereas here it is Form class. Both are the same thing.
OOPS concept:

1. Inheritence:
Inheritence is the process of getting all the properties of the base class within the new class that you are deriving from. Well, its just a shortcut of getting everything pre-defined or created. So it just ensures reusability. Nothing else. Just once a class is defined, it can be inherited instantly to another class and can be used with their function whenever you require to use them. Only the exception if you define a class as NOTINHERITABLE, that class cannot be inherited. So hey hey.. until you are hard enough to give your code to other programmers, you can surely make your code Inheritable... Now lets look at the code below. public class A
public void show()
msgbox ("Hello") ' Messagebox function defined already within your scope...
end sub
end class

public class B
inherits A
end class

Now if you define an object of B, it can definitely call the show function defined within A.

2. Polymorphism: The term signifies, that more than one form. How in case of OOPS, polymorphism can exist. Well, the support of this feature is because of OVERLOADING and OVERRIDING. The term overloading means, existence of more than one function in the same scope with same name. Well, just think, suppose we dont support overloading... just functions.. in that case, you have to define functions individually differently. So there may exist only one function say sum with some number of arguments. Now suppose you have defined a function sum as
public function sum(byval a as Integer, ByVal b as Integer) as Integer
sum=a+b
end Function
in this case, the function sum takes two arguments and returning the sum of both. Well, the condition is nice enough to sum up two numbers. Well, after defining this function suppose some requirement comes, where the function can sum up 3 numbers. Just now the problem begins. Now you need to name the function which you are to create differently. That means separate indentifier to separate functions.. You may think what will be the problem with this. Now suppose you have named a function which is working almost similarly but having different names and you need to remember all of their names. Here comes the benefit of Overloading where similar functions can be named identical but they should be having one common difference. The difference lies on their Argument List. Eg.
public overloads function sum(byval a as Integer,ByVal b as Integer)as Integer
Return a+b
end Function

public overloads Function sum(Byval a as Single,ByVal b as Single) as Single
Return a+b
End Function

Here the two functions having the same name but difference in arguments can stay on the same scope, i.e in a scope of a class. Mind that, overloading is a property of a class, so it would not be working if you write it in a standard module.

Another feature of Polymorphism is Overriding... In case of overriding, two functions are having exactly same signature ... but defined differently.. well, this make your all confusion, isnt it.. Well, how could be possible for a class to call the same two function with exactly the same signature...
Well the case is bit different. Well, the two function should stay on different classes, one in the base class, and another in the derived class... The base class will be overriding the derived class functions.. this is overriding....
Now look at the example
Class A
public overridable function abc()
msgbox ("Hii")
end Function
end class
Class B
Inherits A
Public overrides Function abc()
msgbox ("Hello")
end Function
End Class
So here when we call abc with the object of B, it will call the msgbox with hello, and when we create the object of a and call abc, it will call hi...
So we are modifying the definition of already defined function. isn't it beautiful...

I will continue my posts... later on...
Thanks for reading

Bit related about Interfaces and Abstract classes.

So friends, until now, I think you have got the basic idea about the Object Oriented Programming paradigm. With changing atmosphere, it gets rather difficult to make a project worthwhile. So It needs a structured way to maintain the whole project. Every company before its very creation creates their own framework to make data access for their own products much more easier. The architecture what microsoft provides us is also structured. In talking about structure, one thing that comes first in our mind is Interface.

Lets discuss Interfaces a bit further...
Interface: An interface is a class that have only abstract methods and final variables. An abstract method is just a declaration of the signature without actual implementation of its body. Thus when creating an abstract function we dont need to create the body of the function. Its just the declaration of how a function looks line..

abstract Function sum(Byval x as Integer,ByVal y as Integer) as Integer

In the previous declaration, the function body is nothing, i.e, Its not defined yet. Just it would be defined or overridden to its derived classes.
If a class is having any function within it as abstract, the class must also be declared as abstract. Means, whenever we write any of the member function of a class as abstract, we are losing the propoerty of instantiating the object of that class. So the class will also be called as abstract.
abstract Class A
Function a()
End Class
Now Coming to Inheritence, as I mentioned in my earlier post, Inheritence is the process of making a class just derived from another class. Well, this property ensures re usability of code. Now In .NET environment, one class can have only one base, at most... So Multiple Inheritence is not supported in .NET environment. But this can be supported with the help of Interface.
Interfaces are pure abstract classes.. Means in an Interface there may not be any defination of any of its methods so that rather than inheriting from the class we use implementing from it. Just we need to override all of the functions defined within the interface in our class. This is the case of Multiple Inheritence where if you are implementing from interfaces, a single class can have multiple base classes.
Class A
Inherits BaseA 'Inherits can be only one
Implements Base IA,BaseIB 'Implements can be more than one
Public Function Afun() as string Implements BaseIA.Afun
end Function
Public Function Bfun() as string Implements BaseIB.Bfun
end Function
End class
So here a class A is having three base classes, One is which it is derived from and the others are which it is implementing.
Interface can point to the object of the class which it implementing it. means
Dim ba as BaseIA
ba=new A()

This is possible as if the base class can point to derived class objects.
So you will get everything like the base classes in case of implementing and can enjoy the flavour of Multiple Inheritence in VB.NET.....
Isn't it Cool....