Can I create an Interface which have a method have variate type parameters?

A

ABC

Can I create an Interface which have a method have variate type parameters?

I want create an Interface named IDataEditable which have two methods:
LoadRecord and SaveRecord.

As the LoadRecord have variate type parameters based on the record primary
key definition such as:

The product table should be:

LoadRecord(string productCode)

The OrderDetail table should be:

LoadRecord(string OrderNo, string ProductCode)


Should I write the code as follow?

interface IDataEditable
{
void LoadRecord();
void SaveRecord();
}
 
G

Guest

I think you should rethink this structure.
The concept of an interface is that it enables 1 or many objects to be
treated in exactly the same way, however, the requirement of different
parameters means that it cannot treat these method calls in the same way, so
combining these method calls in an interface is pointless.

You could implement:

LoadRecord(object[] args);

Although I wouldn't recommend such a lack of type-safe arguments as a
consumer could pass 1 argument instead of two causing an IndexOutOfRange
exception.

You could implement

LoadRecord(string productCode);
LoadRecord(string orderNumber, string productCode);

// Impl 1
LoadRecord(string productCode)
{
// do stuff
}

LoadRecord(string productCode, string orderNumber)
{
LoadRecord(productCode); // ignore order number
}

Impl 2

// Impl 1
LoadRecord(string productCode)
{
throw new NotSupportedException();
}

LoadRecord(string productCode, string orderNumber)
{
// do stuff
}

But sooner or later Impl2 will be called with only 1 string firing the
NotSupportedException.

I'd recommend you modify this idea to some else, maybe:

LoadRecord(ILoadRecordInfo info)

Creating a new object that provides information about a request for loading
information, this can have an order number and product number etc.

HTH
 
R

rossum

Can I create an Interface which have a method have variate type parameters?

I want create an Interface named IDataEditable which have two methods:
LoadRecord and SaveRecord.

As the LoadRecord have variate type parameters based on the record primary
key definition such as:

The product table should be:

LoadRecord(string productCode)

The OrderDetail table should be:

LoadRecord(string OrderNo, string ProductCode)


Should I write the code as follow?

interface IDataEditable
{
void LoadRecord();
void SaveRecord();
}
No, this will not allow any parameters to be passed to your methods.
Use the "params" keyword instead:

interface IDataEditable
{
void LoadRecord(params string[] values);
void SaveRecord(params string[] values);
}

This will allow you to pass any number of string parameters to your
Interface methods. Each method will need to count the number of its
parameters to determine what action to take.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top