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

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

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.quantity_left = MyFundDataTmp.quantity_left - 1;
}



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



Regards,
Cybertof.
 
Cybertof said:
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.quantity_left = MyFundDataTmp.quantity_left - 1;
}

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

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.
 
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
 
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.quantity_left = MyFundDataTmp.quantity_left - 1;
}
I have the impression if i replace the struct by a class, the assignment
seems to work....

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.Length; i++)
{
aFundExtrace--;
}
 
But you could use:
for (int i = 0; i < aFundExtract.Length; i++)
{
aFundExtrace--;
}


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

B\rgds
100
 
Only in case where aFundExtract is decalred as an array
(FundDataStruct[]
I assume we're dealing with an array here ;)

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_left--;
}

Completely useless, though ;)
 
aFundExtract is a collection of FundDataStruct filled like

FundDataStruct MyFundData;
MyFundData.price = FillItem.Price;
MyFundData.quantity = FillItem.Quantity;
aFundExtract.Add( 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.
 
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 ?
No


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

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
 
Thanks Bogdan.


The explanation is quite simple. Class is a reference type and struct is
 
Back
Top