Link to home
Start Free TrialLog in
Avatar of dekeyzer
dekeyzerFlag for Belgium

asked on

Nesting class

The following line in my program doesn't work :
        a.class2.pvar2 = TextBox1.Text
I don't have access to the property pvar2 in the sub or nested class.

in my form an simple button :
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Class1
        a.class2.pvar2 = TextBox1.Text
    End Sub

in have a simple class.vb
Public Class Class1
    Dim mvar1 As String

    Public Property pvar1() As String
        Get
            Return mvar1
        End Get
        Set(ByVal Value As String)
            mvar1 = Value
        End Set
    End Property

    Public Class class2
        Dim mvar2 As String

        Public Property pvar2() As String
            Get
                Return mvar2
            End Get
            Set(ByVal Value As String)
                mvar2 = Value
            End Set
        End Property

    End Class

End Class
SOLUTION
Avatar of 123654789987
123654789987

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is  a ?
class2 is a class, where is its object ?
sorry i didnt see it

ok got it the property is not static,
so unless and untill an object is made of class2, as is described by 123654789987
or the property should be Static
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dekeyzer

ASKER

Sorry but a new problem has arised when I tried with the "shared solution"

When I make two instances from the same class1 and i put different values in both instances then i have the same value in both.


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim a As New Class1
        Dim b As New Class1
        a.class2.pvar2 = "input A"
        b.class2.pvar2 = "input B"
        MessageBox.Show(a.class2.pvar2)   'the result is input B instead of input A
    End Sub
Yes that is what the shared do..
It shares the varible in all objects.

Other way is that
do something like this in your Class1

Dim b As New Class2

and make a property to get b in class1

something like

 Public Property class2Property() As Class2
        Get
            Return b
        End Get
        Set(ByVal Value As String)
            b= Value
        End Set
    End Property


and so like this

a.class2Property.pvar2