-1

This 3 classes structure is a simplified view of my code :

Base class :

Public Class A

    Public x As Integer

    Protected Function G() as Integer
       Return x
    End Function

    Protected Sub S(value as Integer)
         x = value
    End Sub

    Public Function Test()
         Return x + 10
    End Function

End Class

Subclasses :

Public Class B

    Inherits A

    Public Property Prop As Integer
        Get
            Return G()
        End Get
        Set(ByVal Value As Integer)
            S(Value)
        End Set
    End Property

End Class

Public Class C

    Inherits A

    Public InnerB As New B

End Class

The goal is to be able to code something like that :

Dim B1 as New B
Dim C1 as New C

B1.Prop = 10
C1.InnerB.Prop = 20 'the "x" member inherited from A takes the value 20 for the InnerB object but not for the C1 object.

MsgBox(B1.Test()) ' returns 20. Works!
MsgBox(C1.Test()) 'returns 10 instead of 30.

Is it possible to fill the inherited "x" member from C by calling the "prop" from it's inner class B ?

kloug
  • 25
  • 2

1 Answers1

0

When you set

C1.InnerB.Prop = 20

It actually does

C1.InnerB.x = 20

When you call

C1.Test()

It will access

C1.x

instead of

C1.InnerB.x

You'll need to call

MsgBox(C1.InnerB.Test()) 

Or override the Test function

Public Class C
    Inherits A

    Public InnerB As New B

    Public Overrides Function Test()
         Return InnerB.Test()
    End Function

End Class

But what you are doing is.... weird

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • Thank you for your answer, it works for my example, but not for my real code : The C class contains in fact many inner classes, and all inherits from the A class. To explain what i am doing, it's basically an ORM. Class A represents the database connection manager (contains recordset and movenext/previous methods etc ... ). Each B Class represents a database table and each property represents a field. The C class is an object containing all database tables, I use this class to manage sql "SELECT" request on several tables. Like you said, maybe it's "weird", and another logic may exists. – kloug May 23 '17 at 15:51
  • @kloug I'm sorry if I can't help you more. It's a bit hard to understand what the problem is. – the_lotus May 23 '17 at 15:57
  • Ok I found a solution. Problem was in fact how to access outer class member from inner class. This thread helped me [link](https://stackoverflow.com/questions/8692475/how-to-get-a-value-inside-parent-class-from-child-class-in-nested-classes) – kloug May 24 '17 at 08:02