Operator_Convert

So, here it’s something I don’t like about Operator_Convert.

I have two classes, ClassA and ClassB.
ClassB is a subclass of ClassA.

Now I have to rewrap these classes into 2 new parallel classes which must have easy conversion from the old ones.
So I create NewClassA and NewClassB which is a subclass of NewClassA
I try to post some simple code to better explain:

Protected Class MyClassA

End Class


Protected Class MyClassB Inherits MyClassA

End Class


Protected Class NewMyClassA
	Sub Operator_Convert(c asMyClassA)

	End Sub
End Class


Protected Class NewMyClassB Inherits NewMyClassA
	Sub Operator_Convert(c as MyClassB)
	
	End Sub
End Class

Now I should have it easy to convert an instance of MyClassB into a NewMyClassB, like this:

dim oldB as new MyClassB
dim newB as NewMyClassB = oldB

however the compiler complain as this:

“There are several items with this name and it is not clear which one the call refers to”

Now, I can understand why the compiler is confused: in my code oldB is clearly a MyClassB but it’s also a MyClassA.
However I’d expect the compiler could call the most “specialized” subclass’s Operator_Convert (if we can assume a subclass is a specialization of a class).

Thought?

(Ok, I can solve it using a SharedMethod which explicitly call the right Operator_Convert, but would be more elegant just use the Operator_Convert)

You can solve this with IsA and super.Operator_Convert. Change the definition of the function in NewMyClassB only:

Sub Operator_Convert (c As MyClassA) // Overriding the super
  if c IsA MyClassB then
   dim t as MyClassB = MyClassB( c )
    // Do the B -> B conversion using t
  end if
  super.Operator_Convert( c )
End Sub

Kem, you are very right! It’s so simple…
That’s what happen after a full day of work in a very hot day of August :wink:

Thanks.