Inheritance and interface implementation.

N

Nick Z.

Please take a minute to take a look at this

//here is a simple interface
public interface ISomeInterface
{
string Name
{
get;
}
}

//here is a class that implements the interface and inherits UserControl
public class SomeClass : System.Windows.Forms.UserControl,ISomeInterface
{
//now here is the problem
//I need to implement the Name property of the ISomeInterface
//However, UserConrol class already implements the Name property
//is there any way to implement a new property name maybe Name1
//and specifically point out that Name1 implements
//ISomeInterface.Name
}

Thanks in advance,
Nick Z.
 
G

Guest

Hi Nick,
you need to use explicit interface naming, i.e.

public interface IF1
{
string Name{get;}
}

public interface IF2
{
string Name{get;}
}


class MyClass : IF1, IF2
{
//these cannot be public, otherwise you would have two
//methods with the same name
string IF1.Name
{
get
{
return "IF1 Name";
}
}

string IF2.Name
{
get
{
return "IF2 Name";
}
}
}


public void static main(string[] args)
{
MyClass objX = new MyClass();

//need to cast the MyClass object to correct interface to use
Console.WriteLine("IF1 name is: " + ((IF1)objX).Name);
Console.WriteLine("IF2 name is: " + ((IF2)objX).Name);
}


Hope that helps.
Mark R Dawson
 
N

Nick Z.

Bingo!
Thank you very much!
Hi Nick,
you need to use explicit interface naming, i.e.

public interface IF1
{
string Name{get;}
}

public interface IF2
{
string Name{get;}
}


class MyClass : IF1, IF2
{
//these cannot be public, otherwise you would have two
//methods with the same name
string IF1.Name
{
get
{
return "IF1 Name";
}
}

string IF2.Name
{
get
{
return "IF2 Name";
}
}
}


public void static main(string[] args)
{
MyClass objX = new MyClass();

//need to cast the MyClass object to correct interface to use
Console.WriteLine("IF1 name is: " + ((IF1)objX).Name);
Console.WriteLine("IF2 name is: " + ((IF2)objX).Name);
}


Hope that helps.
Mark R Dawson

:

Please take a minute to take a look at this

//here is a simple interface
public interface ISomeInterface
{
string Name
{
get;
}
}

//here is a class that implements the interface and inherits UserControl
public class SomeClass : System.Windows.Forms.UserControl,ISomeInterface
{
//now here is the problem
//I need to implement the Name property of the ISomeInterface
//However, UserConrol class already implements the Name property
//is there any way to implement a new property name maybe Name1
//and specifically point out that Name1 implements
//ISomeInterface.Name
}

Thanks in advance,
Nick Z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top