Why the inherited interface doesn't work?

Steven Young 261 Reputation points
2022-03-18T09:28:14.603+00:00

I am testing the NTFS Reader dll from
https://sourceforge.net/projects/ntfsreader/

How to build the inherited interface? My code is below

Public Interface INodeNew
Inherits INode
End Interface

Public Sub GetItems()
Dim listValue1 As List(Of INode)
Dim listValue2 As List(Of INodeNew)
Dim driveToAnalyze As New DriveInfo("C")
Dim ntfsReader As New NtfsReader(driveToAnalyze, RetrieveMode.All)
listValue1 = ntfsReader.GetNodes(driveToAnalyze.Name) ' This line works well
MessageBox.Show(listValue1.Count)
listValue2 = listValue1.Cast(Of INodeNew).ToList ' This line will not work,
MessageBox.Show(listValue2.Count)
End Sub

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-03-20T12:14:47.333+00:00

    Based on your code, you have a child class INodeNew and parent class INode.

    NOw the variable listValue1 is of type INode, which is the parent class. You are trying to cast the parent class object to the child class, which is not supported. You can cast a child class object to the parent class, but not the otherway around.

    For e.g. Imaging you have a Class Car, and Child classes Toyota, Ford, BMW etc. Now the objects of Toyota can be coverted to Car, but a Car object can not be converted to a type of Toyota.

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.