C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Visual Studio »

Calling Similar Methods of two Interfaces from a Class.


Posted Date: 30 Apr 2009    Resource Type: Articles    Category: Visual Studio
Author: MukeshMember Level: Bronze    
Rating: 1 out of 5Points: 10



Problem:In practical scenario we have similar methods in two different interfaces. Problem is how to call a particular interface method from a class.

Solution: Just do it by casting the interface within the class , the following code defined the whole story:



interface Department
{
string DepartmentName { get;set;}
void AddDepartment();
void display();
}

interface Qualification
{
string QualificationName { get;set;}
void AddQualification();
void display();
}

class Employee : Department, Qualification
{
private string _deptname;
private string _qualification;

ArrayList DepartmentList = new ArrayList();
ArrayList QualificationList = new ArrayList();


public string QualificationName
{
get { return _qualification; }
set { _qualification = value; }
}

void Qualification.display()
{
Console.WriteLine("Implementation of Display method of Qualification interface");
}

public string DepartmentName
{
get { return _deptname; }
set { _deptname = value; }
}

public void AddDepartment()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enter Dept Name: ");

_deptname = Console.ReadLine();
DepartmentList.Add(_deptname);

}

}

public void AddQualification()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enter Qualification: ");

_deptname = Console.ReadLine();
QualificationList.Add(_deptname);

}

}

void Department.display()
{

for (int i = 0; i < DepartmentList.Count; i++)
{
Console.WriteLine("Department Name :{0}", DepartmentList[i].ToString());
}

}

}


static void Main(string[] args)
{

Console.WriteLine("-----------------------------------------");

Employee obj = new Employee();
obj.AddDepartment();
obj.AddQualification()
((Department)obj).display();
((Qualification).obj).display();

Console.WriteLine("--------------------------------------------");
}




Conclusion: In the above code we have seen that class employee is inheriting interfaces qualification and department each having display method. In the Employee class for implementing the display method we have to use the interface name before it as shown in the code else it will throw error. Now in the main class we will be casting with the particular interface whose display method we want to call. Then only we will be able to get the display method.




Responses

Author: Gaurav Arora    01 May 2009Member Level: Diamond   Points : 1
Hi Mukesh,

You did a very good work.
I hope you will provide more description in your future articles.

Keep it up.

Regards,
Gaurav Arora
Sr. Editor - Dotnetspider


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Similar Interface method calls from a Class Object  .  Oops  .  Interfaces  .  Implement method with same name in two interfaces  .  C# dotnet  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Run the C# file in visual studio command prompt
Previous Resource: Compare from-date and to-date
Return to Discussion Resource Index
Post New Resource
Category: Visual Studio


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use