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