error very strange

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

    error very strange

    a function, whose parameter is a ref to a collection of class I
    private void func(List<Ii){
    for(int n=0;n<MAXVALUE; ++n){

    for(int m=0;m<MAXVALUE; ++m){

    Dosomething();
    }

    i[n].value=5; [******]Eror: can't modify the value of
    System.Collecti ons.Generic.Lis t<program.Form1 .I>.this[int] because it
    is not a variable[/******]
    }


    Thanks

  • Argentino Douglas

    #2
    Re: error very strange

    Any questions or answers ? Looks like nobody is in now.

    Argentino Douglas wrote:
    a function, whose parameter is a ref to a collection of class I
    private void func(List<Ii){
    for(int n=0;n<MAXVALUE; ++n){
    >
    for(int m=0;m<MAXVALUE; ++m){
    >
    Dosomething();
    }
    >
    i[n].value=5; [******]Eror: can't modify the value of
    System.Collecti ons.Generic.Lis t<program.Form1 .I>.this[int] because it
    is not a variable[/******]
    }
    >
    >
    Thanks

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: error very strange

      Argentino Douglas <douglasdavis12 3456789@yahoo.c om.arwrote:
      a function, whose parameter is a ref to a collection of class I
      private void func(List<Ii){
      for(int n=0;n<MAXVALUE; ++n){
      >
      for(int m=0;m<MAXVALUE; ++m){
      >
      Dosomething();
      }
      >
      i[n].value=5; [******]Eror: can't modify the value of
      System.Collecti ons.Generic.Lis t<program.Form1 .I>.this[int] because it
      is not a variable[/******]
      }
      Could you post a short but complete program which demonstrates the
      problem?

      See http://www.pobox.com/~skeet/csharp/complete.html for details of
      what I mean by that.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Argentino Douglas

        #4
        Re: error very strange

        public struct I{
        public int value;
        }

        private void ValuableHelper( List<I>i){

        i=new List<I>();

        for(int n=0;n<MAXVALUE; ++n){

        for(int m=0;m<MAXVALUE; ++m){

        PleaseHelpMe();

        }

        i[n].value=5; //Error is here

        }
        }
        I am coding it as a window application,
        Thanks


        Jon Skeet [ C# MVP ] wrote:
        Argentino Douglas <douglasdavis12 3456789@yahoo.c om.arwrote:
        a function, whose parameter is a ref to a collection of class I
        private void func(List<Ii){
        for(int n=0;n<MAXVALUE; ++n){

        for(int m=0;m<MAXVALUE; ++m){

        Dosomething();
        }

        i[n].value=5; [******]Eror: can't modify the value of
        System.Collecti ons.Generic.Lis t<program.Form1 .I>.this[int] because it
        is not a variable[/******]
        }
        >
        Could you post a short but complete program which demonstrates the
        problem?
        >
        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: error very strange

          Argentino Douglas <douglasdavis12 3456789@yahoo.c om.arwrote:
          public struct I{
          public int value;
          }
          >
          private void ValuableHelper( List<I>i){
          >
          i=new List<I>();
          >
          for(int n=0;n<MAXVALUE; ++n){
          >
          for(int m=0;m<MAXVALUE; ++m){
          >
          PleaseHelpMe();
          >
          }
          >
          i[n].value=5; //Error is here
          >
          }
          }
          I am coding it as a window application,
          Well, firstly, that's *not* a short but complete program. Please read
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


          However, the cause of your problem is that you've got a struct - a
          value type - and you're trying to change a value which is returned from
          an indexer. You can't do that, and even if you did, it wouldn't do what
          you wanted it to.

          i[n] returns a value. If you modify that value, it won't change the
          contents of the list at all.

          Now, if I were a reference type, you'd be modifying the value *referred
          to* by the reference in the list, and that would be a different matter.

          This kind of problem is just one reason why mutable value types are
          generally a bad idea.

          Oh, and regards your other post: you can't expect replies *that*
          quickly. You only left your original message for 9 minutes before
          posting again!

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...