Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface as parameter

Status
Not open for further replies.

guttaboys

Programmer
Oct 28, 2003
17
NO
Hi

Are you not allowed to use an Interface type as a parameter?

public bool setPayment(IPayment payment)
{...}

I get this message from the compiler:
"Inconsistent accessibility: parameter type 'OilLib.IPayment' is less accessible than method 'OilLib.DBFacade.setPayment(OilLib.IPayment)'"

Any comments?

What other way can I solve this so that I not need to
set a spesific object(class) that implements the interface. (Abstract class or a Base class??)
 
How did you declare your Interface? If I recall correctly, you can't specify an accessor constraint on it -- it is always public (wouldn't make sense otherwise).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
My interface look like this:

interface IPayment
{
string getInfo();
DateTime getPaymentDate();
double getValue();
int getInvoiceNr();
byte getRateNr();
}
 
You should define a class that is derived from IPayment and the type of that class should be passed to SetPayment();
An interface defines a contract. A class or struct that implements an interface must adhere to its contract.
Code:
interface IControl
{
	void Paint();
}
interface IDataBound
{
	void Bind(Binder b);
}
public class EditBox: Control, IControl, IDataBound
{
	public void Paint() {...}
	public void Bind(Binder b) {...}
}
In this example, the Paint() method from the IControl interface and the Bind() method from IDataBound interface are implemented using public members on the EditBox class.
C# provides an alternative way of implementing these methods that allows the implementing class to avoid having these members be public. Interface members can be implemented using a qualified name:
Code:
public class EditBox: IControl, IDataBound
{
	void IControl.Paint() {...}
	void IDataBound.Bind(Binder b) {...}
}
Interface members implemented in this way are called explicit interface members because each member explicitly designates the interface member being implemented. Explicit interface members can only be called via the interface.For example, the EditBox’s implementation of the Paint() method can be called only by casting to the IControl interface.
Code:
class Test
{
	static void Main() {
		EditBox editbox = new EditBox();
		editbox.Paint();	// error: no such method
		IControl control = editbox;  // cast EditBox
		control.Paint();	// calls EditBox’s Paint implementation
	}
}
-obislavu-
 
obislavu -

I thought you could do this -- use an interface as a parameter? I would expect you to be able to pass an instance of a class that implements that interface, and within the method you would only be able to access the methods of the parameter defined by the interface.

IOW, if the interface defines methods A, B & C, you would be limited to using them, and you wouldn't be able to call method D on the parameter. So implementing IPayment:
Code:
public class CMyPayment : IPayment
{
   // Implementation of IPayment goes here
   // getPaymentDate, getInvoiceNr, etc.

   // Other methods, not in the interface:
   public bool IsDeadbeat()
   {
      return false;
   }
}
And the method you're trying to call:
Code:
Public void MyMethod(IPayment ThePayment)
{
   // Call method defined in interface
   int InvoiceNr = ThePayment.getInvoiceNr();

   // Call method not in interface -- should not compile
   bool IsDeadbeat = ThePayment.IsDeadbeat();
}
and to call it:
Code:
{
   // Instantate class that implements IPayment
   CMyPayment MyPayment = new CMyPayment(); 

   // Call method that takes an IPayment
   SomeClassInstance.MyMethod(MyPayment);
}

Of course, maybe what guttaboys is looking for is an abstract base class, where you can extend it, and use it as a parameter type. The dividing line between the two can be very fine...

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top