By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,104 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,104 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Cybertof <cy****************@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
100
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" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 15 '05 #3

P: n/a
On 11/6/2003 6:43 PM, Cybertof wrote:
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[i]--;
}

Nov 15 '05 #4

P: n/a
100
> But you could use:
for (int i = 0; i < aFundExtract.Length; i++)
{
aFundExtrace[i]--;
}


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

B\rgds
100

Nov 15 '05 #5

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


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

Nov 15 '05 #6

P: n/a
100
> > Only in case where aFundExtract is decalred as an array
(FundDataStruct[]
aFundExtract;), though.


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 ;)
Nov 15 '05 #7

P: n/a
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.

In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
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.

Nov 15 '05 #8

P: n/a
On 11/6/2003 9:40 PM, 100 wrote:
I assume we're dealing with an array here ;)

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


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

Nov 15 '05 #9

P: n/a
On 11/6/2003 10:18 PM, Cybertof wrote:
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

Nov 15 '05 #10

P: n/a
Thanks Bogdan.
In article <#X**************@TK2MSFTNGP09.phx.gbl>, lachu@-no-
spam.please.icslab.agh.edu.pl says...
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

Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.