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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Towhid
  • 618
  • 2
  • 5
  • 19

2 Answers2

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
competent_tech
  • 44,465
  • 11
  • 90
  • 113
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • @ekkis more context required - I don't understand the question – Marc Gravell Jul 24 '12 at 21:05
  • I've reformulated the question here: http://stackoverflow.com/questions/11656743/accessing-shared-parent-fields-properties-in-nested-classes - by the way, what trick did you use to get the @ekkis in the message? I can't seem to do the same in my reply to you... it takes it out! – ekkis Jul 25 '12 at 19:03
  • @ekkis it takes it out (as a first word etc) for you because it is *automatically* replying to me. It is my post, etc. – Marc Gravell Jul 25 '12 at 19:10
  • ah. I see. So I couldn't reply to someone else's reply to you made here... which obviates multi-party conversations – ekkis Jul 25 '12 at 20:58
  • 1
    @ekkis yes you could, and it that case it would have removed the `@foo`. It **only** did that because you were replying to the post owner – Marc Gravell Jul 25 '12 at 21:53
  • 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. – Brain2000 Jun 28 '17 at 18:45
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.

D_Bester
  • 5,723
  • 5
  • 35
  • 77