• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

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
0
dekeyzer
Asked:
dekeyzer
  • 4
2 Solutions
 
123654789987Commented:
Try this
Dim a as new Class1.Class2
a.pvar2 = TextBox1.Text
0
 
armoghanCommented:
what is  a ?
class2 is a class, where is its object ?
0
 
armoghanCommented:
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
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
armoghanCommented:
this class will work

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
        Shared mvar2 As String

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

    End Class

End Class
0
 
dekeyzerAuthor Commented:
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
0
 
armoghanCommented:
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
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now