Wednesday, May 7, 2008

Events in C#

(Notes from HFC#)

Steps in Event execution
1)We need an event for event arguments

eg-> public class BallEventArgs : EventArgs

=> EventArgs is an empty class.It has no public members.Its sole purpose is to allow
your event arguments to be passed to the event handlers that use it .

2)Need to define the event in the class that'll raise it .

eg-> public event EventHandler BallInPlay

=>after EVENT keyword comes EVENTHANDLER.It is needed to tell object subscribing to event that
their event handlers need to take 2 parameters,an object named SENDER and an EVENTARGS reference names e,
and have no return value(void).

3)The subscribing classes need EventHandler methods

eg-> void ball_BallInPlay(object sender,EventArgs e )

=>Standard Naming Convention for EventHandlers -> NameOfObjectReference,followed by UNDERSCORE ,followed by
NameOfEvent.

4)Each individual object subscribes to the event

eg-> ball.BallInPlay += new EventHandler(ball_BallInPlay)

=>The += EVENT tells C# subscribe an event handler to an event.

5)A Ball Object raises its event to notify subscribers that it's in play

ex- if (BallInPlay !=null) BallInPlay(this,e)

Note: If you raise an event with no handlers ,it'll throw an exception.

Virtual / Overridable methods in .NET

Virtual / Overridable methods are those which may or may not implemented in the derived class,but they must have some implementation in the base class.

When a derived class method overrides its baseclass method the signature should be same as in the base class, this includes the modifier, parameters and the return type of the method, any modifications in the derived class will lead to a compilation error.

If method is declared virtual in the base class then it is not mandatory to override that function in the derived class. In fact that method need not be a part of the derived class at all. However if the method with the same signature is present in the derived class then it needs clarify whether it is overriding the base class member or not.

a)If the method in the derived class is ment to override the base class member then it needs to have the keyword 'override'

b)If the method in the derived class is not ment to override the base class member then it needs to have the keyword new [Shadows in vb.net] , this is called 'Hiding the Base Class Member'

c)If the method in the derived class has the same name as in the base class but if it has a different signature then it is not required to state either override or new since this method neither overrides not hides the base class method.


These options are not available for an Abstract method, a method declared Abstract in the Base Class must ob overridden in the derived class, it cannot hide the base class member using the 'new' keyword.

Thursday, April 17, 2008

Intro to Design Patterns

Design Principle ->Identify the aspects of application that vary and seperate them from what stays the same i.e.
=>take the parts that vary and encapsulate them,so that later you can alter or extend those parts which v ary without affecting who don't.

Tuesday, May 8, 2007

Scrolling in DataGrid

in aspx page use "div" tag
ex:

*****div tag starting **********
id="scroll" style="height: 25em;overflow-y: auto; "> "

*******datagrid tag***********
****end of div tag*****

Monday, May 7, 2007

Handling Errors for asp.net application

To CREATE ERROR LOG FOR APPLICATION -:

1)create ErrorLog.txt in web application directory
2)create a cs file in project and copy this class


public class ErrorDesc
{
private string sLogFormat;
private string sErrorTime;
public ErrorDesc()
{
sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
//this variable used to create log filename format "
//for example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear+sMonth+sDay;
}
public void ErrorLog(string sPathName, string sErrMsg)
{
StreamWriter sw = new StreamWriter(sPathName,true);
sw.WriteLine(sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}


now whenever in web page we want to handle try catch use :

try{
*******code****
}

catch(Exception ex)
{
ErrorDesc err=new ErrorDesc();
err.ErrorLog(Server.MapPath("ErrorLog.txt"),ex.Message+ ex.StackTrace);
}

Thursday, April 5, 2007

Allowing Only Numerics in TEXTBOX

In aspx page add following script->
function maskBox(e)
{
var keynum = null;
if(window.event){
keynum = e.keyCode;
}
else if(e.which){
keynum = e.which;
}
if(keynum){
if(keynum==8 || keynum==9){return true;}
else
{return isInt(String.fromCharCode(keynum));
}
}
}
function isInt(test)
{
var ints = "0123456789";
if(ints.indexOf(test)>-1)
{
return true;
}
return false;
}
then in page load just add the following line

textBox1.Attributes.Add("OnKeyDown","return maskBox(event);");

Code to Remove duplicate values from ArrayList

arr ->ArrayList with multiple values ,some unique and some repeating ex. {1,2,3,1,2,4,5}

for(i=0;i < arr.Count-1;i++)
{
for(j=i+1;j < arr.Count;j++)
{
if(arr[i].Equals(arr[j]))
{
arr.RemoveAt(j);
j = j - 1;
}
}
}

On executing code above arr={1,2,3,4,5}