Collection and struct address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PauloFor

    #1

    Collection and struct address

    Hi have :

    struct A
    {
    public int val;
    }

    class OtherClass
    {
    private lis = new ArrayList();
    public void Add(ref A element)
    {
    lis.Add(element );
    }

    public void SumAll(int num)
    {
    for (int i=0;i<lis.Count ;i++)
    {
    A work = (A) lis[i]t;
    work.val += num;
    lis[i] = work;
    }
    }

    }

    in code :

    A test1 = new A();
    A test2 = new A();

    test1.val = 5;
    test2.val = 7;
    OtherClass.Add( ref test1);
    OtherClass.Add( ref test2);
    OtherClass.SumA ll(10);

    here I want :
    test1.val = 15 and test1.val = 17;

    but I get :
    test1.val = 5 and test2.val = 7;

    What kind of collection is rigth for this ?


    thks,
    Paulo.







  • Mattias Sjögren

    #2
    Re: Collection and struct address

    [color=blue]
    >What kind of collection is rigth for this ?[/color]

    No other collection will give you that behavior. You would have to
    make A a class to make it work that way. When A is a struct you always
    end upp passing copies of the data around (even if you use ref
    parameters) and changing one copy does not affect others.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Collection and struct address

      Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
      > >What kind of collection is rigth for this ?[/color]
      >
      > No other collection will give you that behavior. You would have to
      > make A a class to make it work that way. When A is a struct you always
      > end upp passing copies of the data around (even if you use ref
      > parameters) and changing one copy does not affect others.[/color]

      Hang on - you *don't* always end up passing copies of the data around.
      The call which passes the struct by reference doesn't make a copy of
      the data. However, passing by reference can never let the called method
      *store* a reference to the original variable anywhere, so after the Add
      method has finished, the variable is "independen t" again.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Mattias Sjögren

        #4
        Re: Collection and struct address

        [color=blue]
        >Hang on - you *don't* always end up passing copies of the data around.[/color]

        Oops, right, I didn't mean to say that, but I realize that I just did.
        :-) Thanks for catching that. What I meant to say was that the
        collection will store a (boxed) copy, whether or not the Add method
        parameter is ref.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...