|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Make the value of MyObjectA equal to the value of MyObjectB?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? 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 Stephany Young wrote:
> If all the members of MyObject are value types then you can use: Thanks for the quick reply.> > 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. > > > 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. 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 Stephany Young wrote:
Show quoteHide quote > Public Class MyObject Thanks again Stephany.> 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 > It is much more clear now. Clone is very similar to a factory. 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
Copying to the clipboard
breaking up a String into an array of chars and adding to datatable loading variable with AutoNumber field value after insert? Option Strict issue Menu - Eliminate The Triangular Arror pointer thing - How to do it? Network Communications not CLS-compliant How to go to the next control? Passing parameter to windows service... Logical Mistake |
|||||||||||||||||||||||