Struct : Left hand side must be variable, property or indexer

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

    Struct : Left hand side must be variable, property or indexer

    Hello,

    Do you know why i get the error
    "Left hand side must be variable, property or indexer"
    with the following code :


    struct FundDataStruct
    {
    internal int quantity;
    internal float price;
    internal int quantity_left;
    }

    foreach( FundDataStruct MyFundDataTmp in aFundExtract )
    {
    MyFundDataTmp.q uantity_left = MyFundDataTmp.q uantity_left - 1;
    }



    I have the impression if i replace the struct by a class, the assignment
    seems to work....



    Regards,
    Cybertof.
  • Jon Skeet [C# MVP]

    #2
    Re: Struct : Left hand side must be variable, property or indexer

    Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
    > Do you know why i get the error
    > "Left hand side must be variable, property or indexer"
    > with the following code :
    >
    >
    > struct FundDataStruct
    > {
    > internal int quantity;
    > internal float price;
    > internal int quantity_left;
    > }
    >
    > foreach( FundDataStruct MyFundDataTmp in aFundExtract )
    > {
    > MyFundDataTmp.q uantity_left = MyFundDataTmp.q uantity_left - 1;
    > }
    >
    > I have the impression if i replace the struct by a class, the assignment
    > seems to work....[/color]

    It's a confusing error message, but the variable in a foreach loop is
    readonly, basically. Even if you *could* do it, it wouldn't change
    anything in aFundExtract, because it's a struct - it would only change
    the local variable.

    --
    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

    • 100

      #3
      Re: Struct : Left hand side must be variable, property or indexer

      Hi Cypertof,
      That is because FundDataStruct is a struct and MyFundDataTmp actually is a
      temporary copy of aFundExtract collection's item.
      Changing data member of that temporary copy won't change anytning in the
      original item. This is the reason for the error message.

      HTH
      B\rgds
      100


      "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
      news:MPG.1a14a6 ad364dd0c99896c 8@msnews.micros oft.com...[color=blue]
      > Hello,
      >
      > Do you know why i get the error
      > "Left hand side must be variable, property or indexer"
      > with the following code :
      >
      >
      > struct FundDataStruct
      > {
      > internal int quantity;
      > internal float price;
      > internal int quantity_left;
      > }
      >
      > foreach( FundDataStruct MyFundDataTmp in aFundExtract )
      > {
      > MyFundDataTmp.q uantity_left = MyFundDataTmp.q uantity_left - 1;
      > }
      >
      >
      >
      > I have the impression if i replace the struct by a class, the assignment
      > seems to work....
      >
      >
      >
      > Regards,
      > Cybertof.[/color]


      Comment

      • Bogdan Lachendro

        #4
        Re: Struct : Left hand side must be variable, property or indexer

        On 11/6/2003 6:43 PM, Cybertof wrote:
        [color=blue]
        > Hello,
        >
        > Do you know why i get the error
        > "Left hand side must be variable, property or indexer"
        > with the following code :
        > foreach( FundDataStruct MyFundDataTmp in aFundExtract )
        > {
        > MyFundDataTmp.q uantity_left = MyFundDataTmp.q uantity_left - 1;
        > }
        > I have the impression if i replace the struct by a class, the assignment
        > seems to work....[/color]

        Thats what the docs for foreach say. If you use foreach on elements
        which are value types they are effectively readonly inside the loop.
        But you could use:
        for (int i = 0; i < aFundExtract.Le ngth; i++)
        {
        aFundExtrace[i]--;
        }

        Comment

        • 100

          #5
          Re: Struct : Left hand side must be variable, property or indexer

          > But you could use:[color=blue]
          > for (int i = 0; i < aFundExtract.Le ngth; i++)
          > {
          > aFundExtrace[i]--;
          > }
          >[/color]

          Only in case where aFundExtract is decalred as an array (FundDataStruct[]
          aFundExtract;), though.

          B\rgds
          100



          Comment

          • Bogdan Lachendro

            #6
            Re: Struct : Left hand side must be variable, property or indexer

            On 11/6/2003 9:07 PM, 100 wrote:
            [color=blue]
            > Only in case where aFundExtract is decalred as an array (FundDataStruct[]
            > aFundExtract;), though.[/color]

            I assume we're dealing with an array here ;)

            Comment

            • 100

              #7
              Re: Struct : Left hand side must be variable, property or indexer

              > > Only in case where aFundExtract is decalred as an array
              (FundDataStruct[][color=blue][color=green]
              > > aFundExtract;), though.[/color]
              >
              > I assume we're dealing with an array here ;)[/color]

              Not quite right, though :-)
              We are dealing with something implementing IEnumarable

              If we want to fool the compiler we can do
              foreach( FundDataStruct MyFundDataTmp in aFundExtract )
              {
              FundDataStruct temp = MyFundDataTmp;
              temp.quantity_l eft--;
              }

              Completely useless, though ;)


              Comment

              • Cybertof

                #8
                Re: Struct : Left hand side must be variable, property or indexer

                aFundExtract is a collection of FundDataStruct filled like

                FundDataStruct MyFundData;
                MyFundData.pric e = FillItem.Price;
                MyFundData.quan tity = FillItem.Quanti ty;
                aFundExtract.Ad d( MyFundData );
                (etc...many times with different values)

                Do you mean there is no way to modify the content of the collection
                using a foreach loop and a structure ? I don't really understand why, as
                each MyFundDataTmp would be "the same" as one of the FundDataStruct
                items (or maybe just a local copy ?....)
                How to make the "local copy" be a reference to the real item ?
                Maybe using a class ?
                Is there a way to do it with a struct ?

                Also, how do you explain that if i replace the structure with a class, i
                do not get the error message anymore ?


                Thanks a lot,
                Cybertof.



                In article <MPG.1a149b66b4 f78857989a39@ms news.microsoft. com>,
                skeet@pobox.com says...
                [color=blue]
                > It's a confusing error message, but the variable in a foreach loop is
                > readonly, basically. Even if you *could* do it, it wouldn't change
                > anything in aFundExtract, because it's a struct - it would only change
                > the local variable.
                >[/color]

                Comment

                • Bogdan Lachendro

                  #9
                  Re: Struct : Left hand side must be variable, property or indexer

                  On 11/6/2003 9:40 PM, 100 wrote:
                  [color=blue][color=green]
                  >>I assume we're dealing with an array here ;)[/color]
                  > Not quite right, though :-)
                  > We are dealing with something implementing IEnumarable[/color]

                  I know, but I'm used to use 'a' prefix for arrays ;)

                  Comment

                  • Bogdan Lachendro

                    #10
                    Re: Struct : Left hand side must be variable, property or indexer

                    On 11/6/2003 10:18 PM, Cybertof wrote:
                    [color=blue]
                    > Do you mean there is no way to modify the content of the collection
                    > using a foreach loop and a structure ? I don't really understand why, as
                    > each MyFundDataTmp would be "the same" as one of the FundDataStruct
                    > items (or maybe just a local copy ?....)
                    > How to make the "local copy" be a reference to the real item ?
                    > Maybe using a class ?
                    > Is there a way to do it with a struct ?[/color]

                    No
                    [color=blue]
                    >
                    > Also, how do you explain that if i replace the structure with a class, i
                    > do not get the error message anymore ?[/color]

                    The explanation is quite simple. Class is a reference type and struct is
                    a value type.
                    You can change something inside the instance of some class, because the
                    reference you're dealing with is the same reference that is used in the
                    collection.
                    Structs/value types are in such situation readonly, because before you
                    get your MyFundDataTmp variable it has to be unboxed - so you don't have
                    the acces to the real value in the collection (you can't change it).
                    It's a value type after all ;)

                    regards,
                    Bogdan



                    Comment

                    • Cybertof

                      #11
                      Re: Struct : Left hand side must be variable, property or indexer

                      Thanks Bogdan.


                      In article <#XyYmALpDHA.24 56@TK2MSFTNGP09 .phx.gbl>, lachu@-no-
                      spam.please.ics lab.agh.edu.pl says...
                      The explanation is quite simple. Class is a reference type and struct is[color=blue]
                      > a value type.
                      > You can change something inside the instance of some class, because the
                      > reference you're dealing with is the same reference that is used in the
                      > collection.
                      > Structs/value types are in such situation readonly, because before you
                      > get your MyFundDataTmp variable it has to be unboxed - so you don't have
                      > the acces to the real value in the collection (you can't change it).
                      > It's a value type after all ;)
                      >
                      > regards,
                      > Bogdan
                      >[/color]

                      Comment

                      Working...