How to get a value inside parent class from child class (in nested classes)?
Asked Answered
S

2

6

I have Class1 and class2 which is inside class1, VB.NET code:

Public Class class1
    Public varisbleX As Integer = 1
    Public Class class2
        Public Sub New()
            'Here GET the value of VariableX
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New class2
    End Sub
End Class

I want to access varisbleX from class2, code in VB.net or C# is appreciated, Thanks.

Soothsayer answered 1/1, 2012 at 9:4 Comment(0)
R
9

The inner class (class2) is not associated with any specific instance of the outer class (class1). T access fields etc, you will need to first have an explicit reference to a class1 instance, probably passing it in via the constructor. For example, it could be:

Public Class class1
    Public varisbleX As Integer = 1
    Public Class class2
        Public Property Parent As class1

        Public Sub New(oParent As class1)
            Me.Parent = oParent
            Console.WriteLine(oParent.varisbleX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New class2(Me)
    End Sub
End Class
Reenareenforce answered 1/1, 2012 at 9:8 Comment(6)
@ekkis more context required - I don't understand the questionReenareenforce
I've reformulated the question here: #11657243 - by the way, what trick did you use to get the @Wiltonwiltsey in the message? I can't seem to do the same in my reply to you... it takes it out!Wiltonwiltsey
@Wiltonwiltsey it takes it out (as a first word etc) for you because it is automatically replying to me. It is my post, etc.Reenareenforce
ah. I see. So I couldn't reply to someone else's reply to you made here... which obviates multi-party conversationsWiltonwiltsey
@Wiltonwiltsey yes you could, and it that case it would have removed the @foo. It only did that because you were replying to the post ownerReenareenforce
We had this same issue, but with thousands of classes that needed to access the parent functions/properties. Happened when we converted our software from VB6 to VB.NET. We ended up using a shared variable. Then we needed multi-threading on IIS with a custom managed handler, so we switched to a shared property that pulls an HttpContext variable stored when the parent class is first initialized. Works beautifully in production with millions of hits every day.Toland
V
0

If you only need a few variables you can pass the variable(s) as a parameter when initializing Class2.

Public Class Class1

    Public VariableX As Integer = 1

    Public Class Class2
        Public Sub New(ByVal VariableX As Integer)
            'Here GET the value of VariableX
            Debug.Print(VariableX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New Class2(VariableX)
    End Sub

End Class

This way Class2 doesn't have access to all of Class1's variables and properties; only what you explicitly give it. Usually we don't want the child class to be in control of the parent class. So this method provides that separation.

Vanhomrigh answered 8/10, 2015 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.