473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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
10 6159
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, Is there a setting/property for displaying a checkbox label (or radiobutton label) on the left hand side instead of the right hand side? Or am I limited to no text in the label - take on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.