[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

I need to enforce an interface AND have base functionality in C#

I need to force child classes to override certain virtual functions and have a certain amount of functionality in the base class.

Since I need member variables and function bodies, I can not use an abstract class.  Therefore I can not use an abstract method.  Then, since the best I can do is virtual functions, those can be overridden optionally in the child classes.

What I think need is a pure virtual function.  Am I right?

How do I implement this in the base class?  I am having trouble using the format I expected to use and have seen on the web:

public virtual void DisplayCancelOpMessage() = 0;

I get a "; expected" error from the compiler.

Thanks for the help,
newbieweb

thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
2 Solutions
 
OBonioCommented:
>> Since I need member variables and function bodies, I can not use an abstract class.

I tihnk you mean you can't use an interface.  You can do both of these things in an abstract class.
0
 
Richard LeeSoftware EnthusiastCommented:
A pure virtual function in c# is as follows:

public abstract void DisplayCancelOpMessage();

How i normally solve your dilemna is having an interface which is implemented by an abstract class then provide a body for the methods or properties i need to implement then declare the others as abstract / pure virtual if you prefer this C++ term.
0
 
Anurag ThakurCommented:
to implement your requirement >> I need to enforce an interface AND have base functionality in C#
abstract class provides a perfect solution
enforce the interface by providing pure virtual functions and base functionality support is already there in the abstract class
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now