Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a question about inheritance.

I've got a bunch of classes that get reflected into a larger project dynammically at run time. All the classes need to have a standard set of methods and properties to interact with this larger project and a lot of overhead code is similar between the classes, so I have a base class that handles the properties and methods, and then a child class that takes care of the specific code for each block via inheritance.

Here's the problem:
In the base class, there is a property called "Memory" declared as an Object. This is used inside the child class to store data, and it needs to be able to contain any type of data, so I have a class called "InternalMemory" which that Memory property gets converted to. This needs to happen the same way for all my child classes, so I'd like to handle it in the base class, but the type it gets converted to needs to be defined in the child class. Is there any way to do this?

Here's some simplified example code.

Public MustInherit Class BlockOverhead
    Public Property Memory As Object
        Get
            Return BlockMemory
        End Get
        Set(value As Object)
            BlockMemory = value
        End Set
    End Property
    Protected BlockMemory As New Object

    Public Sub TryExecute()
        '==========================================================================
        'Try to import the block memory
        '==========================================================================
        InternalBlockMem = TryCast(BlockMemory, Object.GetType(InternalBlockMem))

        If IsNothing(InternalBlockMem) Then
            InternalBlockMem = New InternalMemory
        End If
        '==========================================================================
        'Try to execute the block
        '==========================================================================
        Execute()

        '==========================================================================
        'Save the block memory
        '==========================================================================
        BlockMemory = New Object
        BlockMemory = InternalBlockMem

    End Sub
    Protected MustOverride Sub Execute()
End Class

Public Class Magic1
    Inherits BlockOverhead
    Public Class InternalMemory
        'Create block's memory vars here.
        Public OutValue1 As Integer
        Public OutValue2 As Boolean
    End Class

    Protected Shadows InternalBlockMem As New InternalMemory
    Protected Overrides Sub Execute()
        'Do some stuff to InternalblockMemory
        InternalBlockMem.OutValue1 = 0
        InternalBlockMem.OutValue2 = True
    End Sub
End Class

Public Class Magic2
    Inherits BlockOverhead
    Public Class InternalMemory
        'Create block's memory vars here.
        Public Test As New List(Of String)
        Public Value As Decimal
    End Class

    Protected Shadows InternalBlockMem As New InternalMemory
    Protected Overrides Sub Execute()
        'Do some stuff to InternalblockMemory
        InternalBlockMem.Test.Clear()
        InternalBlockMem.Test.Add("Stuff")
        InternalBlockMem.Value = InternalBlockMem.Value + 1
    End Sub
End Class


What I have tried:

I've tried shadowing, but formatting in the base class modified the base class instance of the variable.

Tried google for several hours. No luck there either.
Posted
Updated 24-Oct-17 7:06am

1 solution

Sounds like you're looking for generics:
Generic Types in Visual Basic (Visual Basic) | Microsoft Docs[^]
VB.NET
Public MustInherit Class BlockOverhead(Of TMemory As {Class, New})
    Public Property Memory As TMemory = New TMemory
    
    Public Sub TryExecute()
        Dim myMemory As TMemory = Memory
        If myMemory Is Nothing Then
            myMemory = New TMemory
        End If
        
        Execute(myMemory)
        Memory = myMemory
    End Sub
    
    Protected MustOverride Sub Execute(ByVal memory As TMemory)
End Class

Public Class Magic1
    Inherits BlockOverhead(Of InternalMemory)
    
    Public Class InternalMemory
        Public OutValue1 As Integer
        Public OutValue2 As Boolean
    End Class
    
    Protected Overrides Sub Execute(ByVal memory As InternalMemory)
        memory.OutValue1 = 42
        memory.OutValue2 = True
    End Sub
End Class
 
Share this answer
 
Comments
Member 11950959 24-Oct-17 17:59pm    
That is exactly what i needed. Thanks a ton.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900