Subject: Some confusion in these code
Posted By: hpatel_it Post Date: 8/30/2006 8:19:35 AM
Hi,

I have read out one sample online article about Interface & Abstract Class. In that article all are mentioned in good way and easily understandable but I have a problem in understanding following code.

Article Link :
http://www.codeproject.com/csharp/A...SInterfaces.asp

In Following code, Lines which are marked by RED color its not understandablel by me.

In short , in first function why we need to use Casting and while in second function why we dont need to use casting.

Can you please help me out for this ?

Code :

private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try
{

IEmployee emp;

Emp_fulltime2 emp1 = new Emp_fulltime2();
//has to be casted because of the interface!
emp = (IEmployee) emp1;

emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object
MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method
MessageBox.Show(emp.CalculateWage().ToString());


}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void cmdAbstractExample_Click(object sender,
System.EventArgs e)
{

Employee emp;
//no casting is requird!
emp = new Emp_Fulltime();


emp.ID = "2244";
emp.FirstName= "Maria" ;
emp.LastName = "Robinlius" ;
MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method
MessageBox.Show(emp.CalculateWage().ToString());

}

Thanks,
Hiren Patel


Go to topic 48711

Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180