Results 1 to 7 of 7

Thread: copying the value of a reference type

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    copying the value of a reference type

    I've got a class, lets call it Person, which needs to keep some data (in the form of another class which we will call PersonData) in one of its properties - the problem is that I will have several instances of this person class and they all need their own 'copy' of the data (just in memory, its not a file or anything).Hhow would I do this? At the moment because everything involved is a Reference type, I'm assuming that if I were to create 2 instances of my Person classes and assign the same instance of the PersonData class to their properties then when one Person class made a change to the PersonData it would be reflected in the other Person class right? How can I avoid that and make it so that they both have their own version of the data, without having to repeat the procedure that fetches the data in the first place?

    I'll explain my exact situation just in case it helps, but this is more of a general question as I could probably get around it in this case by copying the byte array I receive from the server (hopefully that will make sense once you read the explanation below) :
    In this case the data I am talking about copying is actually received from a server via TcpClient. The thing is, when the client requests the data from the server over the network, the server has to go off and query several other servers etc so I do not want to be requesting data from the server every single time as that would be a big waste of resources and time. So when a request is made and data is received back from the server, several of these Person class instances will be created (in reality, its a few different classes but that doesnt make a difference for this example) and each need to have access to the full data that the server has sent back but in some cases the Person instances might need to modify the data and this is where the problem appears as they will all be working on the same data.

    I've found this MemberWiseClone method which I am just reading up on at the moment but perhaps someone can tell me if that is the route I should be going down or if there is another way? http://msdn.microsoft.com/en-us/libr...wiseclone.aspx

    Cheers
    Chris
    Last edited by chris128; Jan 12th, 2010 at 06:05 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: copying the value of a reference type

    It sounds like you need to create a copy constructor. Just add a method called Copy that returns a new instance of the object.

    The PersonData could have this method, and the method would create a New PersonData, set it up with the information out of the current class, and return it. The key is that you have to have a New object, or else you will just have a new address to the same old object.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: copying the value of a reference type

    But how does this method get a copy of the data if the data that it is using is also a reference type?

    For example, lets say the PersonData class has a property named RawData and this a reference type that does not implement any clone/copy method
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: copying the value of a reference type

    This is a question that can only be answered on a case by case basis. Let's say that you have a ClassA object that has a reference to a ClassB object that has a reference to a ClassC object. If you were to make a copy of the ClassA object, should you copy the ClassB reference or the ClassB object? If you copy the ClassB object, should you then copy the ClassC reference or the ClassC object? If you copy the ClassC object, what about the references that it contains? Do you see where this is going?

    If you want to create a deep copy, where does it stop? There is no single answer to this question. It depends completely on the circumstances. If the default was to create a deep copy of everything then creating a copy of a single object might end up cascading to copy many thousands of other objects, which is not likely to be anyone's intention. As such, the default is to shallow copy and and deep copy behaviour is wholly and solely up to the individual developer to implement.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: copying the value of a reference type

    Yeah I can understand why it works the way it does, and I understand how I would clone a class that I had created myself if it only used value types for its properties. If I have created a class that has a reference type for one of its properties and this reference type is not one that I have created (part of the .NET framework for example) then is there any way I can copy that property? Like I said, in this particular case I suppose I can just use Array.Copy when the bytes are received from the server and then build my objects from separate byte arrays, but I'm sure there must be other situations where this type of thing is necessary
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: copying the value of a reference type

    If you want to make a copy of an object that doesn't provide that functionality for you then you must simply create a new object of the same type and assign the property values of the new object from the old. Another alternative, and one that may be necessary to copy private data, is to serialise the existing object and then deserialise to create a new object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: copying the value of a reference type

    Ultimately, you must have access to either the class, or the members of the class, sufficient to create a new instance of the class. However, it is rare that this is not the case. If you have the bytes coming in, then you have the rawest form of the data, so use it as many times as you want.

    The serialize/deserialize option is a good one, but not one that I would reach for first. However, since you are already serializing data to send it across TCP, you are at least familiar with that. The object sent by the server is not the same object as the object received, it is just an identical twin constructed from the bytes. But that is the most work of all the options.

    There's no way around it. You have to build a copy method, and it has to return a new object. The steps that go on behind the scene will have to be as complex as necessary, and in some cases, that can be very complex indeed.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width