In this case I use both as inheritance and interfaces as the latter allows me to refer to quite unrelated classes but in a common way. The method call is usually followed by a series of select case, case isa xyz, case isa vwx etc selections.
Without knowing the details, my first thought upon seeing your comment about using select case statements was, “he needs to rethink this design.” But that’s not what you asked…
I’d have to test, but if you assign your subclass to an int1 variable first, it might force the version you want.
var myInt1 as Int1 = mySubclass
doSomething( myInt1 )
You got me curious, so I just tried this. Because you didn’t write any “actual code,” I’m assuming it looks something like this:
Dim c As New class1
Dim cc As New CustomClass1
Dim cc2 As New CustomClass1
cc.doSomething(cc2)
class1 is the parent
customClass1 is a subclass of class1 with a single method: Public Sub doSomething(param1 as class1)
int1 is an interface, also with a single method: Public Sub doSomething(param as int1)
CustomClass1 has the int1 Interface applied to it so that both methods appear.
When I run my project, I get a compile error saying there is more than one method with that name but that this does not match the available signatures.
Now that said, you can cast the parameter like this to get it to use a particular method:
I had a slightly different implementation (in attached), where cc wasn’t an Int1, but I think the result is the same. Of course the problem I had was deep in other code and not so simple to extract, but it was continually accessing the wrong overloaded method - or so I thought.
This part of the code works fine - and illustrates that the initial casting is key.
Var c1 As class1 = New subclass1
Var c2 As subClass1 = New subClass1
Var c3 As int1 = New subClass1
Var cc1 As New cc
cc1.doSomething(c1) //param as class1
cc1.doSomething(c2) //param as subClass1
cc1.doSomething(c3) //param as int1
I’m still trying to figure out why my earlier code was giving the wrong answer, possibly the original casting wasn’t as expected though I’m not sure how that would have happened.