Solved

Nesting class

Posted on 2004-10-27
250 Views
Last Modified: 2010-04-23
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
Question by:dekeyzer
    6 Comments
     
    LVL 10

    Assisted Solution

    by:123654789987
    Try this
    Dim a as new Class1.Class2
    a.pvar2 = TextBox1.Text
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    what is  a ?
    class2 is a class, where is its object ?
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    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
     
    LVL 18

    Accepted Solution

    by:
    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
     

    Author Comment

    by:dekeyzer
    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
     
    LVL 18

    Expert Comment

    by:armoghan
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now