Jump to content
  • Advertisement
Sign in to follow this  
cignox1

[.net] Cloning complex objects (i.e. strings)

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a remoting systems that creates objects on the server and make them accessible from the clients. Clients need those informations wether the server is running or not, so I thought to make them cloning those objects (mainly strings) locally. My problem is that I don't know how to clone strings (and DateTime)... Is there a way? In addition, our system is already working, because this requirement is a recent one and I would like, if possible, a solution that doesn't require me to rewrite thousands of lines... Thank you!

Share this post


Link to post
Share on other sites
Advertisement
System.String has a Clone() member function. System.DateTime is a value type, so "cloning" is as simple as doing an assignment.

Share this post


Link to post
Share on other sites
Thank you, but following the reference String.Clone() simply returns a reference to the string, it does not perform an independent copy. I've found the method Copy(), that should do the job...
As for what concerns DateTime: I didn't know it was a value type, thank you :-)

Share this post


Link to post
Share on other sites
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.


I was wondering the same thing, but didn't want to sound like a fool in case the answer was really obvious.

Share this post


Link to post
Share on other sites
Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.


Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced. That means that if the remote object that created them is not reachable, those strings are not reachable too, and this is not what I want. This is why I want to copy them in order to have a local copy instead than only a reference to something that may or may not be still alive.
Of course, I may very well be wrong about the behaviour of remote references, and if this is the case, please correct me because the code to update would be quite huge and avoid it would save me a lot of time :-)

Share this post


Link to post
Share on other sites
But aren't you sending them over a network connection anyways? There is no way that the references are preserved over a network connection, I would think.

Anyways, maybe once you have the connection, just write all the strings you need to file, so that the client has a cache somewhere that it can load when the server isn't connected.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
But aren't you sending them over a network connection anyways? There is no way that the references are preserved over a network connection, I would think.

Anyways, maybe once you have the connection, just write all the strings you need to file, so that the client has a cache somewhere that it can load when the server isn't connected.


For the copy I would use string.Copy(). I suppose that the returned string is in client space. I tried with one of my classes and it works, but I didn't tested it in case of connection lost. Using the file is not an option because there are thousands of these objects coming from the server during the execution of the app because they come from a database.

Share this post


Link to post
Share on other sites
Quote:

Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced.

One either side of the network, yes, strings are referenced. But to send them across the network, they are decomposed into bytes and shoved into the network stream. The string that the client gets is not a reference to the string on the server. It is a completely different string, that happens to have the same characters in it. You don't need to clone it, nor do you need to clone the string you send from the server.

And once the string "gets there," it is there. If the connection drops, the string will not go away. In effect, there no difference in lifetime or operational behavior between these two strings:

string stringMadeLocally = "Hello, world!";
string stringFromNetwork = GetHelloWorldStringFromNetwork();

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced.

One either side of the network, yes, strings are referenced. But to send them across the network, they are decomposed into bytes and shoved into the network stream. The string that the client gets is not a reference to the string on the server. It is a completely different string, that happens to have the same characters in it. You don't need to clone it, nor do you need to clone the string you send from the server.

And once the string "gets there," it is there. If the connection drops, the string will not go away. In effect, there no difference in lifetime or operational behavior between these two strings:

string stringMadeLocally = "Hello, world!";
string stringFromNetwork = GetHelloWorldStringFromNetwork();


Well, if this is the truth, I took a good decision by delaying the update :-)
I thought that the var was just a reference to a proxy object (or something like this) but actually this explains why remotable classes have to be serializable :-)

Thank you very much!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!