Reputation: 14467
I have this interface and a class implementing it:
interface Scraper
{
string DateToUrl(DateTime date);
}
class ScraperA: Scraper
{
string Scraper.DateToUrl(DateTime date)
{
return "some string based on date";
}
}
I'd like to test it. I try to add this method to ScraperA
:
public void JustATest()
{
DateTime date = new DateTime(2011, 5, 31);
string url = DateToUrl(date);
Console.WriteLine(url);
}
I put that in the class definition, but the compiler complains that it can't find DateToUrl
. Why?
Upvotes: 2
Views: 140
Reputation: 269628
By prefixing the method name with Scraper
in the declaration you're implementing the interface explicitly.
This means that the method is essentially invisible unless it's called via the Scraper
interface.
Your options:
Remove the Scraper
prefix in the declaration. The method can then be called normally:
public string DateToUrl(DateTime date)
{
return "some string based on date";
}
Cast your instance to the interface before calling the method:
public void JustATest()
{
DateTime date = new DateTime(2011, 5, 31);
string url = ((Scraper)this).DateToUrl(date);
Console.WriteLine(url);
}
Upvotes: 5
Reputation: 22284
Remove the "Scraper" part of "Scraper.DateToUrl":
interface Scraper
{
string DateToUrl(DateTime date);
}
class ScraperA: Scraper
{
string DateToUrl(DateTime date)
{
return "some string based on date";
}
}
Upvotes: 0