---
title: "Issue with generics, interfaces, and casting"  
description: "Issue with generics, interfaces, and casting"  
author: "Takeshi Okada"  
published: 2013-06-11  
updated: 2013-06-12  
canonical: https://www.mindstick.com/forum/1033/issue-with-generics-interfaces-and-casting  
category: "vb.net"  
tags: ["vb.net"]  
reading_time: 2 minutes  

---

# Issue with generics, interfaces, and casting

Hi Expert,\
I [recently added](https://answers.mindstick.com/qa/97987/how-many-wetlands-in-india-were-recently-added-to-the-ramsar-site-list) an [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) to some custom [user controls](https://www.mindstick.com/forum/343/logon-and-logoff-user-controls-how-to-set-isauthenticated-is-true) I have implemented. The interface is pretty basic. It has one method that [supports](https://www.mindstick.com/blog/12043/how-to-create-a-killer-mobile-app-that-supports-your-brand) chaining:\

```
Public Interface IMyInterface(Of T As WebControl)    Function DoSomething() As TEnd Interface
```

\
The implementations are also pretty basic:\

```
Public Class MyCustomControl    Inherits CompositeControl    Implements IMyInterface(Of MyCustomControl)Public Function DoSomething() As MyCustomControl _    Implements IMyInterface(Of MyCustomControl).DoSomething    ' do stuff    Return MeEnd Class
```

**\**Everything [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why) up to this point. The issues arise when I attempt to loop over a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of controls that all implement the IMyInterface interface, like so:\

```
Dim myList = New List(Of IMyInterface(Of WebControl))myList.Add(someCustomControl)myList.ForEach(Sub(i) i.DoSomething())someCustomControl is a MyCustomControl which implements IMyInterface(Of MyCustomControl) instead of IMyInterface(Of WebControl).
```

\
I [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) this error on the second line (where I try to add **someCustomControl**):\
Option Strict On disallows [implicit](https://www.mindstick.com/interview/827/what-are-implicit-objects-list-them) conversions from '**MyCustomControl**' to '**IMyInterface**(Of WebControl)'.\
Is there any way to get around this error? I am close to having it working but I do not know enough about [generics](https://www.mindstick.com/articles/12420/generics-in-c-sharp-with-example) to get beyond this point.\
Thanks in advance for any recommendations or solutions.

## Replies

### Reply by AVADHESH PATEL

Hi Takeshi,\
You will need to cast the object before adding it:\
**myList.Add(CType(someCustomControl, IMyInterface(Of WebControl)))**\
You may also want to concider making the interface not generic and your "DoWork" method return type as the interface itself.\
**Public Interface IMyInterface** **Function DoSomething() As IMyInterface****End Interface****\**When you have to specify the type in the interface definition it kind of takes away from the power of interfaces (not having to know about the implementation).\


---

Original Source: https://www.mindstick.com/forum/1033/issue-with-generics-interfaces-and-casting

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
