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. variable which is assing class value and array.sort problem

variable which is assing class value and array.sort problem

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
9 Posts 4 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.
  • K Offline
    K Offline
    karayel_kara
    wrote on last edited by
    #1

    i have a class and i assing this class value then when i sorted this value or class value, both sorted example code folowing : int[] ggg=new int[5]; int[] ggg1=new int[5]; ggg = networks.layers[i].noron[j].sbaglanti; ggg1 = ggg; Array.Sort(ggg); how can i solve this problem ? regards

    S T C 3 Replies Last reply
    0
    • K karayel_kara

      i have a class and i assing this class value then when i sorted this value or class value, both sorted example code folowing : int[] ggg=new int[5]; int[] ggg1=new int[5]; ggg = networks.layers[i].noron[j].sbaglanti; ggg1 = ggg; Array.Sort(ggg); how can i solve this problem ? regards

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      That is because System.Array is a reference type. When you assign ggg1 = ggg, both ggg and ggg1 refer to the same array instance, so calling Array.Sort on either reference will change that instance. Check out MSDN[^] or search for value types and reference types in Google.

      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      K 1 Reply Last reply
      0
      • K karayel_kara

        i have a class and i assing this class value then when i sorted this value or class value, both sorted example code folowing : int[] ggg=new int[5]; int[] ggg1=new int[5]; ggg = networks.layers[i].noron[j].sbaglanti; ggg1 = ggg; Array.Sort(ggg); how can i solve this problem ? regards

        T Offline
        T Offline
        tom572007
        wrote on last edited by
        #3

        have you tryed : Array.Copy(ggg, ggg1, 5);

        1 Reply Last reply
        0
        • S S Senthil Kumar

          That is because System.Array is a reference type. When you assign ggg1 = ggg, both ggg and ggg1 refer to the same array instance, so calling Array.Sort on either reference will change that instance. Check out MSDN[^] or search for value types and reference types in Google.

          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

          K Offline
          K Offline
          karayel_kara
          wrote on last edited by
          #4

          i understand but if arrays is referans value, how can i solve my problem?

          S 1 Reply Last reply
          0
          • K karayel_kara

            i have a class and i assing this class value then when i sorted this value or class value, both sorted example code folowing : int[] ggg=new int[5]; int[] ggg1=new int[5]; ggg = networks.layers[i].noron[j].sbaglanti; ggg1 = ggg; Array.Sort(ggg); how can i solve this problem ? regards

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            int[] ggg = new int[5];
            ggg = networks.layers[i].noron[j].sbaglanti;
            int[] ggg1 = (int[]) ggg.Clone();
            Array.Sort(ggg);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            K 1 Reply Last reply
            0
            • K karayel_kara

              i understand but if arrays is referans value, how can i solve my problem?

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              You typically find a way to clone the source reference. In this case, Array.Copy should do the trick. Bear in mind that if the elements in the array themselves are reference types, then Array.Copy won't clone them.

              Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

              T 1 Reply Last reply
              0
              • S S Senthil Kumar

                You typically find a way to clone the source reference. In this case, Array.Copy should do the trick. Bear in mind that if the elements in the array themselves are reference types, then Array.Copy won't clone them.

                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                T Offline
                T Offline
                tom572007
                wrote on last edited by
                #7

                That's what i said 10 minutes ago :^)

                1 Reply Last reply
                0
                • C CPallini

                  int[] ggg = new int[5];
                  ggg = networks.layers[i].noron[j].sbaglanti;
                  int[] ggg1 = (int[]) ggg.Clone();
                  Array.Sort(ggg);

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  K Offline
                  K Offline
                  karayel_kara
                  wrote on last edited by
                  #8

                  thanks friends. i solved with array.copy(); regards

                  T 1 Reply Last reply
                  0
                  • K karayel_kara

                    thanks friends. i solved with array.copy(); regards

                    T Offline
                    T Offline
                    tom572007
                    wrote on last edited by
                    #9

                    :-D

                    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