Questions regarding objects, assignments, byval, byref, etc... 
Author Message
 Questions regarding objects, assignments, byval, byref, etc...

Say I have an object loaded as the property of another object called
objA, how could I set an object property of a different object called
objB so that if I assign "objB.Object = new ClassNameHere()" the New()
will have taken effect in both objB.Object and objA.Object.  It seems
to be very easy to get property modifications of objB.Object to flow
through to objA.Object, but we haven't yet figured out how to change
what objA.Object points to when we change what objB.Object points to.

Here's a look at test scenario that demonstrates what I'm referring
to.  The following is meant to simulate a more complex scheme that we
are currently using.  It involves a "data-tier" set of classes and an
"interface-tier" which serves to make it easier to modify the data
tier.

Say I have the following data-tier objects:

'''''''''''''''''''''''''''''''''''
Public Class clsDataParent
    Private objDataObjectChild As New clsData()

    Public Sub LoadDataChild(ByVal strString As String)
        objDataObjectChild.StringValue = strString
    End Sub

    Public ReadOnly Property DataChild() As clsData
        Get
            DataChild = objDataObjectChild
        End Get
    End Property
End Class

Public Class clsData
    Private strString As String

    Public Property StringValue() As String
        Get
            StringValue = strString
        End Get
        Set(ByVal Value As String)
            strString = Value
        End Set
    End Property
End Class
'''''''''''''''''''''''''

And the following interface tier:

''''''''''''''''''''''''''
Public Class clsInterface
    Private objData As clsData

    Public Property DataObject() As clsData
        Get
            DataObject = objData
        End Get
        Set(ByVal Value As clsData)
            objData = Value
        End Set
    End Property

    Public Sub SetData(ByRef objValue As clsData)
        objData = objValue
    End Sub
End Class
''''''''''''''''''''''''''

Now, say in code we tried to implement the objects as follows:

'''''''''''''''''''
Dim objDataParent As New clsDataParent()
Dim objData As New clsData()
Dim objInterface As New clsInterface()

objDataParent.LoadDataChild("Testing 1")

objInterface.SetData(objDataParent.DataChild)
MsgBox(objDataParent.DataChild.StringValue)

objInterface.DataObject.StringValue = "Testing 2"
MsgBox(objDataParent.DataChild.StringValue)

objData.StringValue = "Testing 3"
objInterface.SetData(objData)
MsgBox(objDataParent.DataChild.StringValue)

objData = Nothing
objDataParent = Nothing
objInterface = Nothing
''''''''''''''

The desired effect is to see three message boxes that read "Testing
1", "Testing 2", and "Testing 3".  What we're seeing, however, is that
although changes to the PROPERTIES of the interface's data object will
carry through to the originally loaded data-tier, it is impossible to
change the actual OBJECT and have it carry through to the data tier.
So, we're getting message boxes that read "Testing 1", "Testing 2",
and "Testing 2".

What we're looking to do is make the clsData object in clsInterface be
a true pointer to the clsDataParent.clsData object that it received
its assignment from.  We need to be able to change property values
using the interface object and have those take effect in the data
tier, AND we need the ability to set an interface object's clsData
object to another object and have that reflected in the data tier as
well.

We've played with different things.  Originally we did everything
right through the Property, but as this did not work we thought that a
method with a ByRef parameter might help (hence,
clsInterface.SetData()).

I'm struggling with this, but I figured this would be an easy one for
the experts to straighten out.

thanks in advance,
-matt



Sun, 06 Mar 2005 02:17:46 GMT  
 Questions regarding objects, assignments, byval, byref, etc...
Hi.

Instead of:

   objData.StringValue = "Testing 3"
   objInterface.SetData(objData)

do this:

   objData = objInterface.DataObject
   objData.StringValue = "Testing 3"

Hope this helps,

   Adrian

This posting is provided "AS IS" with no warranties, and
confers no rights.

Quote:
>-----Original Message-----
>Say I have an object loaded as the property of another
object called
>objA, how could I set an object property of a different
object called
>objB so that if I assign "objB.Object = new ClassNameHere
()" the New()
>will have taken effect in both objB.Object and

objA.Object.  It seems
Quote:
>to be very easy to get property modifications of
objB.Object to flow
>through to objA.Object, but we haven't yet figured out
how to change
>what objA.Object points to when we change what

objB.Object points to.
Quote:

>Here's a look at test scenario that demonstrates what
I'm referring
>to.  The following is meant to simulate a more complex
scheme that we
>are currently using.  It involves a "data-tier" set of
classes and an
>"interface-tier" which serves to make it easier to
modify the data
>tier.

>Say I have the following data-tier objects:

>'''''''''''''''''''''''''''''''''''
>Public Class clsDataParent
>    Private objDataObjectChild As New clsData()

>    Public Sub LoadDataChild(ByVal strString As String)
>        objDataObjectChild.StringValue = strString
>    End Sub

>    Public ReadOnly Property DataChild() As clsData
>        Get
>            DataChild = objDataObjectChild
>        End Get
>    End Property
>End Class

>Public Class clsData
>    Private strString As String

>    Public Property StringValue() As String
>        Get
>            StringValue = strString
>        End Get
>        Set(ByVal Value As String)
>            strString = Value
>        End Set
>    End Property
>End Class
>'''''''''''''''''''''''''

>And the following interface tier:

>''''''''''''''''''''''''''
>Public Class clsInterface
>    Private objData As clsData

>    Public Property DataObject() As clsData
>        Get
>            DataObject = objData
>        End Get
>        Set(ByVal Value As clsData)
>            objData = Value
>        End Set
>    End Property

>    Public Sub SetData(ByRef objValue As clsData)
>        objData = objValue
>    End Sub
>End Class
>''''''''''''''''''''''''''

>Now, say in code we tried to implement the objects as
follows:

>'''''''''''''''''''
>Dim objDataParent As New clsDataParent()
>Dim objData As New clsData()
>Dim objInterface As New clsInterface()

>objDataParent.LoadDataChild("Testing 1")

>objInterface.SetData(objDataParent.DataChild)
>MsgBox(objDataParent.DataChild.StringValue)

>objInterface.DataObject.StringValue = "Testing 2"
>MsgBox(objDataParent.DataChild.StringValue)

>objData.StringValue = "Testing 3"
>objInterface.SetData(objData)
>MsgBox(objDataParent.DataChild.StringValue)

>objData = Nothing
>objDataParent = Nothing
>objInterface = Nothing
>''''''''''''''

>The desired effect is to see three message boxes that
read "Testing
>1", "Testing 2", and "Testing 3".  What we're seeing,
however, is that
>although changes to the PROPERTIES of the interface's
data object will
>carry through to the originally loaded data-tier, it is
impossible to
>change the actual OBJECT and have it carry through to
the data tier.
>So, we're getting message boxes that read "Testing
1", "Testing 2",
>and "Testing 2".

>What we're looking to do is make the clsData object in
clsInterface be
>a true pointer to the clsDataParent.clsData object that
it received
>its assignment from.  We need to be able to change
property values
>using the interface object and have those take effect in
the data
>tier, AND we need the ability to set an interface
object's clsData
>object to another object and have that reflected in the
data tier as
>well.

>We've played with different things.  Originally we did
everything
>right through the Property, but as this did not work we
thought that a
>method with a ByRef parameter might help (hence,
>clsInterface.SetData()).

>I'm struggling with this, but I figured this would be an
easy one for
>the experts to straighten out.

>thanks in advance,
>-matt
>.



Sun, 06 Mar 2005 17:04:13 GMT  
 Questions regarding objects, assignments, byval, byref, etc...
I guess what I'm basically asking here is, how would I do a pointer to
a pointer in VB.NET.  I'm thinking that that's what is required
above....

-matt



Sun, 06 Mar 2005 22:36:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. objects byval and byref

2. ByRef And ByVal for Object

3. Objects ByRef or ByVal?

4. Help: Setting objects equal (Byval, not Byref)

5. ByVal or ByRef with objects

6. Passing Parameters - A ByRef vs ByVal Question

7. ByVal or Byref : performance question ?

8. ByVal or Byref : performance question ?

9. Override ByRef with ByVal

10. ByVal / ByRef

11. byVal vs ByRef

12. Change Standard from ByVal to ByRef

 

 
Powered by phpBB® Forum Software