Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:24:59+00:00 2026-06-03T04:24:59+00:00

I have inheritance structure: Foo implements IGraphNode inherits IGraphItem . Foo , IGraphItem /

  • 0

I have inheritance structure: Foo implements IGraphNode inherits IGraphItem.

Foo, IGraphItem/IGraphNode, and the implementation for IGraphItem/IGraphNode all reside in separate assemblies. I am using an inversion of control container, so the project I’m working in has a reference to the first two (Foo and IGraphItem/IGraphNode), but not the implementation of IGraphItem/IGraphNode. I also have Option Strict on as it is required for this project (turning if off didn’t fix the problem). I’m using .NET 3.5.

I am passing a IGraphItem around and I have code that looks like this:

Public Sub ProcessItem(of IGraphItem)(item As IGraphItem)
    If TypeOf item Is Foo Then
        Dim f1 = CType(item, Foo) 'Compiler error
        Dim f2 = DirectCast(item, Foo) 'Compiler error

        'This is what I'm currently having to do. It works.
        Dim f = CType(CType(item, IGraphNode), Foo)

        'Do stuff
    End If
End Sub

Any idea why I’m having to do this? I should add that TryCast works, but since we’ve just confirmed that item‘s type is Foo, I don’t see why I can’t DirectCast it. Shouldn’t it just let me and throw an exception if I’m wrong? Is there a better way to accomplish what I’m trying to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:25:01+00:00Added an answer on June 3, 2026 at 4:25 am

    Your original code compiles without a problem, even when target framework is 3.5.

    The problem with your current code is that you’ve defined a generic method whereas IGraphItem is not the type of your interface but the generic type T which can be any type. But it cannot be another type than T and you’re trying to cast it to type Foo.

    If you would change your method signature to anything else it would work, for instance:

    Public Sub ProcessItem(of IGraphItm)(item As IGraphItem)
    

    I assume that you’re somehow “shadowing” the type IGraphItem of your interface with the generic type IGraphItem in this method.

    It would also work if you would explicitely tell the compiler that item As IGraphItem actually is a item As YourNamespace.IGraphItem.

    I’m sure Jon or Eric could explain it better, but maybe it’s helpful anyway 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following inheritance structure: interface A { void Foo(); } interface B
I have an entity model where the base class in an inheritance structure has
I have a complex inheritance structure in my data model. A majority of classes
I have read some vague statement that virtual inheritance doesn't provide the memory structure
I need little help with inheritance in C++. I have code with same structure
I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure
I have the following inheritance structure: var environment:AvEnvironment = new AvEnvironment(...); addChild(environment); environment.addChild(new Terrain());
I have an issue with mongoid inheritance. I have a Zipcode with inherits from
I have an inheritance structure of objects with begin() and end() as pure virtual
I have 3 classes with the following inheritance structure: <?php class admin { function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.