title: Call a method that requires a derived class instance typed as base class in VB.NET or C#
body: <p>I have two objects - "Spaceship" and "Planet" derived from a base "Obj". I have defined several classes - Circle, Triangle, Rectangle, etc. which all inherit from a "Shape" Class.</p>
  
  <p>For collision detection purposes, I want to give Obj a "shape":</p>
  
  <pre><code>Dim MyShape as Shape
  </code></pre>
  
  <p>So that in "Spaceship" I can:</p>
  
  <pre><code>MyShape = new Triangle(blah,blah)
  </code></pre>
  
  <p>and in "Planet" I can:</p>
  
  <pre><code>MyShape = new Circle(blah,blah)
  </code></pre>
  
  <p>I have a method (overloaded several times) which checks for collisions between different shapes, for example:</p>
  
  <pre><code>public shared overloads function intersects(byval circle1 as circle, byval circle2 as circle) as boolean
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>public shared overloads function intersects(byval circle as circle, byval Tri as triangle) as boolean
  </code></pre>
  
  <p>This works fine when I call the function using the derived classes, for example:</p>
  
  <pre><code>dim A as new circle(blah, blah)
  dim B as new triangle(blah, blah)
  return intersects(A,B)
  </code></pre>
  
  <p>But when I call it using MyShape, I get an error because the method is being passed a "Shape" (rather than the derived type) which the method does not have an overload for.</p>
  
  <p>I could solve it by doing something like:</p>
  
  <pre><code>Public Function Translate(byval MyShape1 as Shape, byval MyShape2 as Shape )as boolean
  if shape1.gettype = gettype(circle) and shape2.gettype=gettype(circle) then ''//do circle-circle detection
  if shape1.gettype = gettype(triangle) and shape2.gettype=gettype(circle) then ''//do triangle-circle detection
  End Function
  </code></pre>
  
  <p>But that seems messy. Is there a better way?</p>