0

I'm getting the following exception:

Unable to cast object of type 'MyClass`1[DerivedClass]' to type 'MyClass`1[BaseClass]'.

An implicit cast is attempted after I try to return an object of type MyClass(of DerivedClass) from a function call. Is there a way to explicitly cast this?


Public Class IClass(Of T)
   GetModel() as T
End Class


Public Class MyClass(Of T)
   Implements IClass (of T)

   Public Function GetModel() As T Implements IClass(Of T).GetModel
      Return _model
   End Function

   'Other methods/properties omitted for brevity

End Class

Public BaseClass
   ...
End Class

Public DerivedClass
   Inherits BaseClass
   ...
End Class

In this example I am attempting to explicitly cast, in my program there is an implicit cast. However both lead to the same result of not being able to cast (though one is at run time and one will not compile)

Dim c1  As New MyClass(Of DerivedClass)
Dim c2 = TryCast(c1, MyClass(Of BaseClass))

Edit: I thought I simplified my example enough to not need to show any code but I guess not based on comments. So I added some example code.

Though I would prefer responses to be written in vb.net, C# is acceptable too (whichever you are more comfortable with).

I am only slightly familiar with the concept of generic variance (only just stumbled upon while attempting to find a solution to my current problem), so if that is the solution a detailed explanation or link would be appreciated.

AXG1010
  • 1,872
  • 3
  • 21
  • 35
  • "Is there a way to explicitly cast" [DirectCast](https://msdn.microsoft.com/en-us/library/7k6y2h6x.aspx) – crush Jul 23 '15 at 17:21
  • 1
    I don't see where you are Inheriting from a base class. Show an example of your function and call to it. – OneFineDay Jul 23 '15 at 17:21
  • @OneFineDay is right. Without inheriting from your base class, any type of cast will fail. – crush Jul 23 '15 at 17:22
  • Is the problem it needs to be `MyClass(Of Out T)` so it is covariant? Whether you can make it covariant to solve this issue depends on what you're doing though. – dmeglio Jul 23 '15 at 17:24
  • 1
    Without [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem, it's not 100% clear where you are getting the error, nor why. But based on the small amount of information you've provided so far, it seems highly likely that you are attempting something along the lines of `MyClass a = (MyClass)new B();` where `class A { }` and `class B : A { }`. If so, then this is a duplicate of the literally hundreds of questions on SO involving C# and variance in generic types. – Peter Duniho Jul 23 '15 at 17:28
  • I've updated the question with some example code. @ Peter Duniho you may be right about variance being my issue, but I have not heard the term until about 5 mins ago. @dman2306 I don't think it's possible to use the "out" keyword on a class...only interfaces. – AXG1010 Jul 23 '15 at 18:00
  • **1.** when attempting to notify commenters with `@`, the user ID is terminated by any space character (` `). If a space immediately follows the `@` character, no one will be notified. **2.** based on your update, you are in fact attempting to use the generic type in an unsupported, variant way. I note that you have an interface involved; if you can make the interface variant, you may be able to get this to work by going through the interface instead of the class type. In any case, please review the many SO Q&A's regarding generic variance; if you have more trouble, post a _specific_ question. – Peter Duniho Jul 23 '15 at 20:42

0 Answers0