Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ICloneable class with strings

ICloneable class with strings

Scheduled Pinned Locked Moved C#
question
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WorkOnlineClick2Wealth
    wrote on last edited by
    #1

    Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.

    L OriginalGriffO D 3 Replies Last reply
    0
    • W WorkOnlineClick2Wealth

      Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      your username f****** sucks a**

      1 Reply Last reply
      0
      • W WorkOnlineClick2Wealth

        Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Can you give us an example of what doesn't work? It's not clear from your question exactly what you are trying to do and what you expect to happen.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        W 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Can you give us an example of what doesn't work? It's not clear from your question exactly what you are trying to do and what you expect to happen.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          W Offline
          W Offline
          WorkOnlineClick2Wealth
          wrote on last edited by
          #4

          If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well

          OriginalGriffO Richard DeemingR 2 Replies Last reply
          0
          • W WorkOnlineClick2Wealth

            If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Show us the code you are using.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            W 1 Reply Last reply
            0
            • W WorkOnlineClick2Wealth

              Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #6

              Because, if you look at the specification of String.Clone, it states; "The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance." Read https://msdn.microsoft.com/en-us/library/system.string.clone(v=vs.110).aspx If you also read the specification for; ICloneable Interface (System)[^] it states that ". It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance." Hope that helps..........

              Dave Find Me On:Web|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject

              W 1 Reply Last reply
              0
              • D DaveAuld

                Because, if you look at the specification of String.Clone, it states; "The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance." Read https://msdn.microsoft.com/en-us/library/system.string.clone(v=vs.110).aspx If you also read the specification for; ICloneable Interface (System)[^] it states that ". It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance." Hope that helps..........

                Dave Find Me On:Web|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject

                W Offline
                W Offline
                WorkOnlineClick2Wealth
                wrote on last edited by
                #7

                :-O I was using .Substring(0) instead of Copy. And have been looking to know why clone and = do not copy the string. Thank you for the info

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Show us the code you are using.

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  W Offline
                  W Offline
                  WorkOnlineClick2Wealth
                  wrote on last edited by
                  #8

                  I am working on converting server code from Java to C#.. Not really ASP but pure C# as this serves only text, the frontend server sends my app a request on port 8080 consisting of user's UserAgent and source IP, and my program decides to serve some text for every 1 in 20 or as defined. Its proprietary code

                  1 Reply Last reply
                  0
                  • W WorkOnlineClick2Wealth

                    If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    WorkOnlineClick2Wealth wrote:

                    Changing the string in object B changes it in object A as well

                    How are you "changing" the string, given that strings in .NET are immutable? :confused:

                    class Foo
                    {
                    public string Bar { get; set; }
                    }

                    Foo a = new Foo { Bar = "Hello" };
                    Foo b = new Foo { Bar = a.Bar };

                    b.Bar = "Goodbye";

                    Console.WriteLine(a.Bar);
                    // Output: "Hello"

                    If you can't show us the code you're using, you need to create a small sample project to reproduce the problem and show us that code instead.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups