Louis van Tonder
Louis van Tonder

Reputation: 3710

Inherit and add properties to child class

I can inherit from a class, and add properties to my new class... that is all good.

Class class1

 Property a as string

End Class

Class class2
 inherits class1

 property b as string

End Class

Dim mytest as new class2

mytest.a = "bleh"
mytest.b = "bah"

How do I add properties to a child class inside a class I am inheriting?

Class class3

 Property a as string

 Class class3_1
   Property c as string
 End Class

End Class

How do I create a new class that enherits class3, but add a property to class3_1, inside my new enherited class?

NEW EDIT WITH ACTUAL NON WORKING CODE

Public Class testclass

    Public Class class1

        Property x As String
        Property firstChildClass As New class1_1

        Public Class class1_1
            Property a As String
        End Class

    End Class

    Public Class inherited_class1
        Inherits class1

        Public Class class1_1  '///// Warning here to use "Shadows" keyword.. but that also does not work... takes away the warning, but still cant access property b when using a new instance of class
            Property b As String
        End Class

    End Class

    Sub test()

        Dim myTestClass As New inherited_class1

        myTestClass.firstChildClass.b = "blah" '///// This does not work...

    End Sub

End Class

Upvotes: 1

Views: 728

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157116

Just include the containing class in the inheritance of the sub class. You have to separately inherit the containing class and the sub class:

Public Class inherited_class1
    Inherits class1

    Public Class class1_2
        Inherits class1_1
        Public Property b() As String
    End Class

End Class


Public Sub test()
    Dim myTestClass As New inherited_class1()

    Dim ic As New inherited_class1.class1_2()
    myTestClass.firstChildClass = ic
    ic.b = "blah"
    '''//// This does not work...
End Sub

Upvotes: 1

Related Questions