Can implemented interfaces be used to determine which overload to call?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
Suppose I have two interfaces and I make a method that is overloaded:

Method(IOne)
Method(ITwo)

If I have 3 classes that inherit from a base class:
Dim b as Base = New OneImpl
Dim c as Base = New TwoImpl
Dim d as Base = New OneTwoImpl

What happens in each of these case:
Method(b)
Method(c)
Method(d)


Presumably I cant just use this, and I will have to interrogate the type myself and call the relevant overload with a cast?

I ask because I actually have 4 classes, 3 interfaces and they overlap somewhat:
ISignable - the request obeys a security procedure
IChargeable - the request holds credit card info
IReconcilable - the request holds daily transaction totals


(I'm also struggling as to whether to name these adjectives or nouns: ISignature, ICreditCardInfo, IReconciliationInfo)

Abstract BaseRequest that all below inherit from:
AuthRequest Implements IChargeable
ChargeRequest Implements ISignable, IChargeable
ReconRequest Implements ISignable, IReconcilable


ChargeRequest could inherit from BasicRequest and just implement ISignable. Dont know if this would help

So my question here is, is this the best way to arrange this and can I get away with making an overloaded ProcessRequest and calling:
ProcessRequest(myRequest)

Is there a way of arranging the overloads so that the reconcile is done if the myRequest is IReconcilable, the charge is done if (blah blah) and the auth is done if.. (balh blah)

Or will it have to be:
if(myREquest Is IReconcilable AndAlso myrequest Is ISignable) Then ProcessSignableCharge(myRequest)
...
 
Suppose I have two interfaces and I make a method that is overloaded:

Method(IOne)
Method(ITwo)

If I have 3 classes that inherit from a base class:
Dim b as Base = New OneImpl
Dim c as Base = New TwoImpl
Dim d as Base = New OneTwoImpl

What happens in each of these case:
Method(b) -> Method(IOne)
Method(c) -> Method(ITwo)
Method(d) -> compile error
Overload resolution failed because no accessible 'Method' is most specific for these arguments
for OneTwoImpl you would have to choose which cast to call by:
VB.NET:
Method(DirectCast(d, IOne))
Won't these interfaces have different intensions and functionality implemented, so the choice be clear to what you want to do if same object can go "both ways"?
 
Won't these interfaces have different intensions and functionality implemented, so the choice be clear to what you want to do if same object can go "both ways"?

Hmm.. thats the hard part. Sometimes, yes, sometimes no..

Both charging and reconciling are secured events, both auth and charge have creditcardsinfo.. I'm thinking if I adopt this form, i'll have to do some IFfing on the type impls anyway. As it is, I just have a base class and 3 derivations with repeated code.. there isnt anything to enforce stuff.. so when it comes to logging, i'd like to call my log stored procedure with:

log(
generic.whatever,
generic is ISignable?generic as SIgnable.signature:null,
generic is IReconcilable?generic as IRecon.reconInfo:null


yep, it's C# ternary.. vb cant do this sort of stuff without getting lengthy
 
All three calls fail actually, I dimmed the variables the quick VB way 'Dim o As New OneImpl' instead of what you asked 'Dim o As base = New OneImpl'. So the error is:
Overload resolution failed because no accessible 'method' can be called without a narrowing conversion
Still that is not the problem you asked about. You could have one public method take 'base' as parameter and use this as box, then check the real object and its implemented interfaces in this method and pass the object along to more specific private methods that handles single interface objects.
 
Back
Top