CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Dictionary Values copy

    Hello everyone,


    Here is my code to make a duplicate value copy of all the Values in a dictionary instance -- not just reference. So, I use CopyTo method.

    My current issue is, I think my code is stupid but do not know how to solve it. My specific concern is,

    1.

    the code Foo[] FooArray = new Foo[3] creates 3 new instances of Foo, the code dic.Values.CopyTo(FooArray, 0) will overwrite the 3 new instances of Foo? So, seems it is stupid to create 3 new instances of Foo in Foo[] FooArray = new Foo[3], but never use them and overwrite them? Any ideas? Is it an issue?

    2.

    Maybe I am wrong, the code Foo[] FooArray = new Foo[3] creates only references variable of Foo class? Not class instances variable themselves?

    Code:
        class Foo
        {
            public int abc;
    
            public Foo (int i)
            {
                abc = i;
            }
            
        }
        
        public static void Main()
        {
    
            Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
            dic.Add(1, new Foo(10));
            dic.Add(1, new Foo(20));
            dic.Add(3, new Foo(30));
    
            Foo[] FooArray = new Foo[3];
    
            dic.Values.CopyTo(FooArray, 0);
    
            return;
        }

    thanks in advance,
    George

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dictionary Values copy

    Code:
    Foo[] FooArray = new Foo[3];
    That does not create 3 Foo instances. It only creates an array that has 3 elements set to null. Have you tried to run it in debugger and look at the values?

    You could also put a message in the constructor of Foo and see that it's not printed.
    Code:
       public Foo(int i)
       {
          abc = i;
          Console.WriteLine("Constructs Foo({0})", i);
       }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: Dictionary Values copy

    Thanks cilu,


    So this code Foo[] FooArray = new Foo[3] will only create 3 references and make them refer to NULL? Then code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class, and assign the 3 NULL references variables in FooArray to point to the instances?

    Quote Originally Posted by cilu
    Code:
    Foo[] FooArray = new Foo[3];
    That does not create 3 Foo instances. It only creates an array that has 3 elements set to null. Have you tried to run it in debugger and look at the values?

    You could also put a message in the constructor of Foo and see that it's not printed.
    Code:
       public Foo(int i)
       {
          abc = i;
          Console.WriteLine("Constructs Foo({0})", i);
       }

    regards,
    George

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dictionary Values copy

    You know, most of the time, the best way is to try it.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: Dictionary Values copy

    Thanks cilu!


    I have tested CopyTo will only copy the reference, not the references, to my surprise. :-)

    Code:
        class Foo
        {
            public int abc;
    
            public Foo (int i)
            {
                abc = i;
            }
            
        }
        
        public static void Main()
        {
    
            Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
            dic.Add(1, new Foo(10));
            dic.Add(2, new Foo(20));
            dic.Add(3, new Foo(30));
    
            Foo[] FooArray = new Foo[3];
    
            dic.Values.CopyTo(FooArray, 0);
    
            // change the 1st one
            FooArray[0].abc = 40;
    
            // output 40
            Console.WriteLine(dic[1].abc);
    
            return;
        }
    Quote Originally Posted by cilu
    You know, most of the time, the best way is to try it.

    regards,
    George

  6. #6
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Dictionary Values copy

    C# works in most cases only with references. For getting duplicates you should implement ICloneable in your business class and call the clone() method for getting a second instance of the object. Thats the usual way in C# as far as I know.
    Useful or not? Rate my posting. Thanks.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dictionary Values copy

    I have tested CopyTo will only copy the reference, not the references, to my surprise. :-)
    Hm, Foo is a class right? That makes it a reference type. So of course it behaves as you observed. In .NET there are two kind of types: value types, copied/passed by value and reference types, copied/passed by reference.

    Make Foo a struct and then see what happens.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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

Featured