Home All Groups Group Topic Archive Search About

Make the value of MyObjectA equal to the value of MyObjectB?

Author
15 Jan 2007 3:12 AM
Bruce
I am a C++ programmer so sorry for the simple VB question.

If I have two instances of the same type of object, how do I make the
value of one equal to the other?

If I do

Dim ObjA as new MyObject
Dim ObjB as new MyObject

ObjA = ObjB


then ObjA is set to the same instance as ObjB.

How do I just make the values equal?   Do I need a CopyTo method for the
object?



--
Bruce E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com

Author
15 Jan 2007 3:31 AM
Stephany Young
If all the members of MyObject are value types then you can use:

  ObjA = ObjB.Clone()

If any of the members are reference types then you needs to handle them in
some other way.

Either way the two instances will not be equal, even though they may have
the same content.


Show quoteHide quote
"Bruce" <Bruce@nospam.com> wrote in message
news:ObLpbLFOHHA.4848@TK2MSFTNGP04.phx.gbl...
>I am a C++ programmer so sorry for the simple VB question.
>
> If I have two instances of the same type of object, how do I make the
> value of one equal to the other?
>
> If I do
>
> Dim ObjA as new MyObject
> Dim ObjB as new MyObject
>
> ObjA = ObjB
>
>
> then ObjA is set to the same instance as ObjB.
>
> How do I just make the values equal?   Do I need a CopyTo method for the
> object?
>
>
>
> --
> Bruce E. Stemplewski
> GarXface OCX and C++ Class Library for the Garmin GPS
> www.stempsoft.com
Author
15 Jan 2007 4:19 AM
Bruce
Stephany Young wrote:
> If all the members of MyObject are value types then you can use:
>
>   ObjA = ObjB.Clone()
>
> If any of the members are reference types then you needs to handle them in
> some other way.
>
> Either way the two instances will not be equal, even though they may have
> the same content.
>
>
>


Thanks for the quick reply.

Some members are other objects.  I would assume that MyObject would have
to implement the Clone method?  Actually I am trying to picture what
such a method would look like.





--
Bruce E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Author
15 Jan 2007 4:41 AM
Stephany Young
Public Class MyObject
    Implements ICloneable

    ....
    ....
    ....
    ....

    Public Function Clone() As MyObject Implements ICloneable.Clone

      Dim _clone as New MyObject

      'Do the stuff here to populate the members of _clone from Me

      'If member X is a value type or a System.String then it is simply
      _clone.X = Me.X

      'If member Y is a reference type and is not a System.String then
      'you need to ensure that you get a copy and not a reference

      'The reason that _clone.X = Me.X works for a System.String is that
      'a System.String is immutable so you get a new copy of it instead
      'of a reference

      'When all the cloning is done simply
      Return _clone

    End Function

  End Class


Show quoteHide quote
"Bruce" <Bruce@nospam.com> wrote in message
news:%23nqpIxFOHHA.324@TK2MSFTNGP06.phx.gbl...
> Stephany Young wrote:
>> If all the members of MyObject are value types then you can use:
>>
>>   ObjA = ObjB.Clone()
>>
>> If any of the members are reference types then you needs to handle them
>> in some other way.
>>
>> Either way the two instances will not be equal, even though they may have
>> the same content.
>>
>>
>
>
> Thanks for the quick reply.
>
> Some members are other objects.  I would assume that MyObject would have
> to implement the Clone method?  Actually I am trying to picture what such
> a method would look like.
>
>
>
>
>
> --
> Bruce E. Stemplewski
> GarXface OCX and C++ Class Library for the Garmin GPS
> www.stempsoft.com
Author
15 Jan 2007 5:33 AM
Bruce
Stephany Young wrote:
Show quoteHide quote
>   Public Class MyObject
>     Implements ICloneable
>
>     ....
>     ....
>     ....
>     ....
>
>     Public Function Clone() As MyObject Implements ICloneable.Clone
>
>       Dim _clone as New MyObject
>
>       'Do the stuff here to populate the members of _clone from Me
>
>       'If member X is a value type or a System.String then it is simply
>       _clone.X = Me.X
>
>       'If member Y is a reference type and is not a System.String then
>       'you need to ensure that you get a copy and not a reference
>
>       'The reason that _clone.X = Me.X works for a System.String is that
>       'a System.String is immutable so you get a new copy of it instead
>       'of a reference
>
>       'When all the cloning is done simply
>       Return _clone
>
>     End Function
>
>   End Class
>


Thanks again Stephany.

It is much more clear now.  Clone is very similar to a factory.



--
Bruce E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Author
15 Jan 2007 6:11 AM
Cor Ligthert [MVP]
Bruce,

Mostly this is done by those objects where is no real copy method with
serialize it.

See at the bottom
http://www.vb-tips.com/dbpages.aspx?ID=7ffd296f-9e81-47e6-88dc-61641f5c8d9d

Cor

Show quoteHide quote
"Bruce" <Bruce@nospam.com> schreef in bericht
news:ObLpbLFOHHA.4848@TK2MSFTNGP04.phx.gbl...
>I am a C++ programmer so sorry for the simple VB question.
>
> If I have two instances of the same type of object, how do I make the
> value of one equal to the other?
>
> If I do
>
> Dim ObjA as new MyObject
> Dim ObjB as new MyObject
>
> ObjA = ObjB
>
>
> then ObjA is set to the same instance as ObjB.
>
> How do I just make the values equal?   Do I need a CopyTo method for the
> object?
>
>
>
> --
> Bruce E. Stemplewski
> GarXface OCX and C++ Class Library for the Garmin GPS
> www.stempsoft.com