blah blah blah is here! blah blah » Close

up0down
link

public interface IValidator
{
void ValidateX(ArrayList al);
void ValidateY(ArrayList al);
}
public class Employee : IValidator
{
public void ValidateX(ArrayList al)
{

}
void IValidator.ValidateY(ArrayList al)
{

}
}
public class A
{
public A()
{
Employee e = new Employee();
}
public void Test1()
{
e.ValidateX( New ArrayList() ); // can call this
e.ValidateY( New ArrayList() ); //CANNOT CALL
}
}
In the class "A" why i was able to call ValidateX() and not ValidateY()?
whats the difference with calling a interface method as :
public void ValidateX(ArrayList al)
{

}
and
void IValidator.ValidateY(ArrayList al)
{

}
?
cheers

last answered 10 months ago

1 answers

up0down
link

In the Employee class, the ValidateX method is implemented normally.
However, ValidateY is implemented using 'explicit interface implementation'. This means that you can only call ValidateY via an IValidator reference - you can't call it via an Employee reference.
To call it, change this line:
e.ValidateY( new ArrayList() ); // notice New should be lower case
with:
IValidator iv = e;
iv.ValidateY( new ArrayList() );
You can also knock this into a single line with:
((IValidator)e).ValidateY( new ArrayList() );

up0down
link

you can also not implement it explicitly.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback