Thursday, November 15, 2007

What is a pattern

"A pattern is a proven solution to a problem in a context."

It solves a problem: Patterns capture solutions, not just abstract principles or strategies.

It is a proven concept: Patterns capture solutions with a track record, not theories or speculation.

The solution isn't obvious: Many problem-solving techniques (such as software design paradigms or methods) try to derive solutions from first principles. The best patterns generate a solution to a problem indirectly--a necessary approach for the most difficult problems of design.

It describes a relationship: Patterns don't just describe modules, but describe deeper system structures and mechanisms.

The pattern has a significant human component (minimize human intervention). All software serves human comfort or quality of life; the best patterns explicitly appeal to aesthetics and utility

Bury the dead Code

http://devlicio.us/blogs/derik_whittaker/archive/2007/11/14/if-the-code-is-dead-then-bury-it.aspx

Tuesday, November 13, 2007

The Life cycle of an ASP.NET page

A page in an ASP.NET application consists of several server controls. These are the fundamental building blocks of an ASP.NET application. The Life cycle of an ASP.NET page, depends on whether the page is requested for the first time or it is a postback. Postback is a process by which a page can request for itself.

When the Page is requested for the first time

The Life Cycle of a page when requested for the first time:

Initializing: During this phase, the server creates an instance of the server control

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving: During this phase, the state information of the control is saved. For example, if a value is set for the control during the Load event, it is embedded in the HTML tag that will be returned to the browser.

Rendering: During this phase, the server creates the corresponding HTML tag for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

Life cycle when the page processed during a postback event

The processing sequence in which a page is processed during a postback event is:

Initializing: During this phase, the server creates an instance of the server control

Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control.

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving state: During this phase, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.

Rendering: During this phase, the server creates the corresponding HTML tag for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

The events associated with the relevant page cycle phases are:

  • Page Initialization: Page_Init
  • View State Loading:LoadViewState
  • Postback data processing: LoadPostData
  • Page Loading: Page_Load
  • PostBack Change Notification: RaisePostDataChangedEvent
  • PostBack Event Handling: RaisePostBackEvent
  • Page Pre Rendering Phase: Page_PreRender
  • View State Saving: SaveViewState
  • Page Rendering: Page_Render
  • Page Unloading: Page_UnLoad

Sunday, September 30, 2007

Static Constructors

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).
Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

class StaticConstructor
{ static StaticConstructor()
{
Console.WriteLine("I am in static Constructor");
}
public StaticConstructor()
{
Console.WriteLine("I am in normal Constructor");
}
}
class Program
{
static void Main(string[] args)
{
StaticConstructor staticConstructor1 = new StaticConstructor();
StaticConstructor staticConstructor2 = new StaticConstructor();
Console.ReadLine();

}
}

How events work in C#

An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).

namespace Event
{
class Program
{
static void Main(string[] args)
{
EventRaiser eventRaiser = new EventRaiser();
ClassA classA = new ClassA();

/*
* Subscribing to an Event
*/
//Events with Static method
eventRaiser.raised += ClassB.Method;

//Events with instantiated object
eventRaiser.raised += classA.Method;

//Method which raise the event
eventRaiser.RaiseEvent();

Console.ReadLine();

}

}

class EventRaiser
{ //Declaring an Event
public event EventHandler raised;

public void RaiseEvent()
{
EventRaiserMethod(null, EventArgs.Empty);
}
private void EventRaiserMethod(object obj, EventArgs e)
{ if (raised != null)
{
Console.WriteLine("I am in the Event");
raised(obj, e);
}
}

public EventRaiser()
{

}
}

class ClassA
{
public void Method(object obj, EventArgs sender)
{
Console.WriteLine("I am in Method of Class A");
}

}

class ClassB
{
public static void Method(object obj, EventArgs sender)
{
Console.WriteLine("I am in Method of Class B");
}
}
}

Monday, September 10, 2007

How to write custom TextBox

namespace StringEdit
{
public partial class StriingEdit : TextBox
{



Boolean isMandatory;
Boolean allowNegative;

ErrorProvider error = new ErrorProvider();

public Boolean AllowNegative
{
get { return allowNegative; }
set { allowNegative = value; }
}



public Boolean IsMandatory
{
get { return isMandatory; }
set { isMandatory = value; }
}
public StriingEdit()
{
InitializeComponent();

}

protected override void OnValidating(CancelEventArgs e)
{


if (this.isMandatory && this.Text == String.Empty)
{
error.SetError(this, ErrorMessage.EmptyTextBox);
MessageBox.Show(ErrorMessage.EmptyTextBox);
}

else if (allowNegative)
{
try
{
if (Single.Parse(this.Text) <= 0)
{
error.SetError(this, ErrorMessage.TextBoxNegValue);
MessageBox.Show(ErrorMessage.TextBoxNegValue);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
base.OnValidating(e);
error.Clear();



}

}
}


namespace StringEdit
{
public class ErrorMessage
{
private static string emptyTextBox = "TextBox is empty";
private static string textBoxNegValue = "Value should greater or equal to zero";

public static string EmptyTextBox
{
get { return ErrorMessage.emptyTextBox; }

}
public static string TextBoxNegValue
{
get { return ErrorMessage.textBoxNegValue; }

}
}
}

for complete running code for this project mail at huzaifa.shabbir@yahoo.com