结构:左侧必须是变量,属性或索引器 [英] Struct : Left hand side must be variable, property or indexer

查看:74
本文介绍了结构:左侧必须是变量,属性或索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


您知道为什么我会收到错误

左手边必须是变量,属性或索引器

,代码如下:

struct FundDataStruct

{

内部数量;

内部浮动价格;

internal int quantity_left;

}


foreach(FundDataStruct MyFundDataTmp in aFundExtract)

{

MyFundDataTmp.quantity_left = MyFundDataTmp.quantity_left - 1;

}


我有一个印象,如果我替换结构一个班级,作业

似乎有用....


问候,

Cyber​​tof。

解决方案

Cyber​​tof< cy **************** @ gmx.net>写道:

你知道为什么我得到错误
左手边必须是变量,属性或索引器
使用以下代码:

struct FundDataStruct
{内部数量;
内部浮动价格;
内部数量_left;
}
foreach(FundDataStruct MyFundDataTmp in aFundExtract)
{/ / MyFundDataTmp.quantity_left = MyFundDataTmp.quantity_left - 1;
}

我有一个印象,如果我用类替换结构,赋值
似乎工作....




这是一个令人困惑的错误信息,但foreach循环中的变量是

readonly,基本上。即使你*可以*做,它也不会改变aFundExtract中的任何东西,因为它是一个结构 - 它只会改变局部变量的



-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


你好Cypertof,

那是因为FundDataStruct是一个结构而MyFundDataTmp实际上是一个

aFundExtract集合项目的临时副本。

更改该临时副本的数据成员不会更改

原始项目中的任何内容。这就是错误信息的原因。

HTH

B \ rgds

$

Cyber​​tof < CY **************** @ gmx.net>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

你好,

你知道为什么我得到错误
左手边必须是变量,属性或索引器
使用以下代码:

struct FundDataStruct
{
内部数量;
内部浮动价格;
内部数量_左右;
}

foreach(在aFundExtract中的FundDataStruct MyFundDataTmp)
{
MyFundDataTmp.quantity_left = MyFundDataTmp.quantity_left - 1;
}

我有一个印象,如果我替换结构通过课程,作业似乎有效....

问候,
Cyber​​tof。



< blockquote>在11/6/2003 6:43 PM,Cyber​​tof写道:

你好,

你知道我为什么会收到错误
左侧必须是变量,属性或索引器,并使用以下代码:
f oreach(FundDataStruct MyFundDataTmp in aFundExtract)
{
MyFundDataTmp.quantity_left = MyFundDataTmp.quantity_left - 1;
}
我有一个印象,如果我用类替换结构,赋值
似乎工作....




这就是foreach的文档说的。如果你在元素上使用foreach

它们是值类型,它们在循环中是有效读取的。

但你可以使用:

for(int i = 0; i< aFundExtract.Length; i ++)

{

aFundExtrace [i] - ;

}


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


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.



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


这篇关于结构:左侧必须是变量,属性或索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆