Object accessed from another Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthickbabu
    New Member
    • Sep 2007
    • 33

    Object accessed from another Class

    In vb.net i create two classes. In First class i define a property to return a single value and in second class define one function it get values from property and return a single value. I call the function when i click the button.

    I create a object for property and use this object to call the property when i press enter in form after entering the number in text box. Values can be insert properly using first class(property) and it return safely. But in second class i create a object for Property and access the variable from first class (that is property defined class). It store only zero here, so it return zero value.

    But if i give shared as variable in first class, this value can access in function through classname.varia blename, because of shared.

    I need to access the variable through object. My code is below. Let me know any wrong in this code

    Code:
    Public Class clsProperty
    
        Public m_Value As Integer
    
        Public Property InsertValues() As Integer
            Get
                Return m_Value
            End Get
            Set(ByVal value As Integer)
                m_Value = value
            End Set
        End Property
    End Class
    
     Public Class clsFunctions
        Public Function DisplayValue() As Integer
            Try
                Dim objProp As New clsProperty
                Dim returnValue As Integer
                returnValue = objProp.m_Value
                Return returnValue
    
            Catch ex As Exception
                Throw ex
            End Try
        End Function
    End Class
    
    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
            Try
    
                Dim objProperty As New clsProperty
                Dim objFunction As New clsFunctions
                objProperty.InsertValues = Val(txtInput.Text)
                txtOutput.Text = objFunction.DisplayValue()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
    End Sub
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    Originally posted by karthickbabu
    In vb.net i create two classes. In First class i define a property to return a single value and in second class define one function it get values from property and return a single value. I call the function when i click the button.

    I create a object for property and use this object to call the property when i press enter in form after entering the number in text box. Values can be insert properly using first class(property) and it return safely. But in second class i create a object for Property and access the variable from first class (that is property defined class). It store only zero here, so it return zero value.

    But if i give shared as variable in first class, this value can access in function through classname.varia blename, because of shared.

    I need to access the variable through object. My code is below. Let me know any wrong in this code

    Code:
    Public Class clsProperty
    
        Public m_Value As Integer
    
        Public Property InsertValues() As Integer
            Get
                Return m_Value
            End Get
            Set(ByVal value As Integer)
                m_Value = value
            End Set
        End Property
    End Class
    
     Public Class clsFunctions
        Public Function DisplayValue() As Integer
            Try
                Dim objProp As New clsProperty
                Dim returnValue As Integer
                returnValue = objProp.m_Value
                Return returnValue
    
            Catch ex As Exception
                Throw ex
            End Try
        End Function
    End Class
    
    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
            Try
    
                Dim objProperty As New clsProperty
                Dim objFunction As New clsFunctions
                objProperty.InsertValues = Val(txtInput.Text)
                txtOutput.Text = objFunction.DisplayValue()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
    End Sub
    I didn't see any line of code that insert value into Property object in your Function object here. You created two objects, one is Property and one is Function. In Function object there is a Property object, this object is obviously has no relationship with the first Property object. Then what is your point when you saying that you can not retrieve value from shared object?
    cheers.

    Comment

    Working...