Reputation: 165
I have created a basic template program helping me understand Interfaces, classes and the such.
What I am trying to achieve is simply calling a method that resides in an inherited class hierarchy.
I am getting this error:
'Book' does not contain a definition for 'BulkOrder' and no extension method for 'BulkOrder' accepting a first argument of type 'Book' could be found(are you missing a using directive or an assembly reference?)
This is the main program
Main
{
BulkBook book2 = new BulkBook(FILLER);
BulkOrder(book2);
}
public static void BulkOrder(Book book2)
{
Console.WriteLine(Filler text);
book2.BulkOrder(); <------- belongs in inherited class
}
This is the inherited class structure
abstract class Publication
various code
class Book : Publication
various code
class BulkBook : Book
various code
public void BulkOrder() <------ method
{
Copies = Copies + BATCH_SIZE;
}
Upvotes: 0
Views: 76
Reputation: 4395
Since BulkOrder
exists in BulkBook
and not Book
you need to cast book2
to BulkBook
:
((BulkBook)book2).BulkOrder();
However you may wish to verify that book2
is in fact a BulkBook
:
if (book2 is BulkBook)
{
((BulkBook)book2).BulkOrder();
}
An alternative solution would be to have the method accept the type BulkBook
rather than Book
:
public static void BulkOrder(BulkBook book2)
{
Console.WriteLine(Filler text);
book2.BulkOrder(); //This should now work
}
Upvotes: 1
Reputation: 155085
class BulkBook : Book
...
public void BulkOrder()
{
Copies = Copies + BATCH_SIZE;
}
The BulkOrder
method only exists in the BulkBook
class, not the Book
class.
Upvotes: 2