variable which is assing class value and array.sort problem
-
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
-
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
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
-
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
-
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
i understand but if arrays is referans value, how can i solve my problem?
-
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
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] -
i understand but if arrays is referans value, how can i solve my problem?
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
-
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
-
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]thanks friends. i solved with array.copy(); regards
-
thanks friends. i solved with array.copy(); regards