为什么会有错误? [英] why is there an error?

查看:83
本文介绍了为什么会有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码产生错误:

"作业的左侧必须是变量,属性或

索引器


----------------------------------- ------------------------


private void ListBox_DrawItem(object sender,DrawItemEventArgs e){


ListBox b =发送者为ListBox;


((IconItem)b.Items [e.Index])。Bounds = new Rectangle(0,0) ,1,1);

}

--------------------------- -------------------------------------------

但以下情况并非如此:


--------------------------- -------------------------------------------

private void ListBox_DrawItem(对象发送者,DrawItemEventArgs e){


ListBox b =发件人为ListBox;


IconItem ii = ((IconItem)b.Items [e.Index]);

ii.Bounds = new Rectangle(0,0,1,1);

}


--------------------------- -------------------------------------------

为什么?

解决方案

Bern< x@x.com>写道:


以下代码产生错误:
赋值的左侧必须是变量,属性或
索引器

-------------------------------------------- ---------------

private void ListBox_DrawItem(object sender,DrawItemEventArgs e){

ListBox b =发送者为ListBox;

((IconItem)b.Items [e.Index])。Bounds = new Rectangle(0,0,1,1);
}
------ -------------------------------------------------- --------------

但以下情况并非如此:

-------------- -------------------------------------------------- ------

private void ListBox_DrawItem(对象发送者,DrawItemEventArgs e){

ListBox b =发送者为ListBox;

IconItem ii =((IconItem)b.Items [e.Index]);

ii.Bounds = new Rectangle(0,0,1,1);
}

------------------------------------------------ ----------------------


如果不知道IconItem是什么就很难确定,

但是我的猜测是它是一个值类型。在这种情况下,您将从C#规范的第14.13.1节中运行




< quote>

当在struct-type中声明的属性或索引器是

的赋值时,与该属性关联的实例表达式或

索引器访问必须是归类为变量。如果实例

表达式被归类为值,则会发生编译时错误。

< / quote>


问题是,如果它* *编译,它将执行第二个代码所做的第二个代码 - 这完全没用。最后你需要这个

你的方法:


b.Items [e.Index] = ii;


否则,Items集合中的值副本根本不会改变




这就是你的原因'如果IconItem是引用类型,那么允许这样做

- 在这种情况下,你确实会做出改变。


-

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

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


我想我会将IconItem声明为一个类,而不是一个结构。

因为把'b.Items [e.Index] = ii;"

导致项目被重绘,从而产生一个无限循环。


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

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

伯尔尼< x@x.com>写道:


以下代码产生错误:
赋值的左侧必须是变量,属性或
索引器

-------------------------------------------- ---------------

private void ListBox_DrawItem(object sender,DrawItemEventArgs e){

ListBox b =发送者为ListBox;

((IconItem)b.Items [e.Index])。Bounds = new Rectangle(0,0,1,1);
}
------ -------------------------------------------------- --------------

但以下情况并非如此:

-------------- -------------------------------------------------- ------

private void ListBox_DrawItem(对象发送者,DrawItemEventArgs e){

ListBox b =发送者为ListBox;

IconItem ii =((IconItem)b.Items [e.Index]);

ii.Bounds = new Rectangle(0,0,1,1);
}

------------------------------------------------ ----------------------

在不知道IconItem是什么的情况下很难确定,但我的猜测是它是一种价值类型。在这种情况下,你将从C#规范的第14.13.1节开始运行以下内容:

< quote>
当一个属性或索引器在struct-type是赋值的目标,与属性或
索引器访问相关联的实例表达式必须归类为变量。如果实例
表达式被归类为值,则会发生编译时错误。
< / quote>

问题是如果它*执行*编译,它会做第二个代码所做的事情 - 这完全是无用的。你的方法最后需要这个:

b.Items [e.Index] = ii;

否则,该值的副本在物品收藏根本没有改变。

这就是为什么如果IconItem是参考类型你将被允许这样做的原因
- 在那里确实如此,你所期待的改变确实会发生。

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



如果我创建以下代码,我就不能失败:


class App

{

static void Blah(object sender,DrawItemEventArgs e)

{

ListBox b =发送者为ListBox;

IconItem ii =((IconItem)b.Items [e.Index]);

ii.Bounds = new Rectangle(0,0) ,1,1);

}

}


class IconItem

{

public Rectangle Bounds;

}

}


那么你能否制作一个简洁的片段来展示你的行为?


问候


Richard Blewett - DevelopMentor
< a rel =nofollowhref =http://www.dotnetconsult.co.uk/weblogtarget =_ blank> http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

以下代码产生错误:

"作业的左侧必须是变量,属性或

indexer"


----------------------------------------- ------------------


private void ListBox_DrawItem(object sender,DrawItemEventArgs e){


ListBox b =发送者为ListBox;


((IconItem)b.Items [e.Index])。Bounds = new Rectangle(0,0,1,1);

}

---------------------- ------------------------------------------------ <无线电通信/>

但以下情况并非如此:


---------------------- ------------------------------------------------ <无线电通信/>

private void ListBox_DrawItem(对象发送者,DrawItemEventArgs e){


ListBox b =发送者为ListBox;


IconItem ii =((IconItem)b.Items [e.Index]);

ii.Bounds = new Rectangle(0,0,1,1);

}


------------------------------------ ----------------------------------

为什么?



the following code produces an error:
"The left-hand side of an assignment must be a variable, property or
indexer"

-----------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

((IconItem) b.Items [e.Index]).Bounds = new Rectangle (0, 0, 1, 1);
}
----------------------------------------------------------------------

But the following does not:

----------------------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

IconItem ii = ((IconItem) b.Items [e.Index]);
ii.Bounds = new Rectangle (0, 0, 1, 1);
}

----------------------------------------------------------------------
Why??

解决方案

Bern <x@x.com> wrote:


the following code produces an error:
"The left-hand side of an assignment must be a variable, property or
indexer"

-----------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

((IconItem) b.Items [e.Index]).Bounds = new Rectangle (0, 0, 1, 1);
}
----------------------------------------------------------------------

But the following does not:

----------------------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

IconItem ii = ((IconItem) b.Items [e.Index]);
ii.Bounds = new Rectangle (0, 0, 1, 1);
}

----------------------------------------------------------------------



It''s hard to know for absolutely sure without knowing what IconItem is,
but my guess is that it''s a value type. In that case, you''re running
into the following from section 14.13.1 of the C# spec:

<quote>
When a property or indexer declared in a struct-type is the target of
an assignment, the instance expression associated with the property or
indexer access must be classified as a variable. If the instance
expression is classified as a value, a compile-time error occurs.
</quote>

The problem is that if it *did* compile, it would do what the second
bit of code does - which is totally useless. You need this at the end
of your method:

b.Items[e.Index] = ii;

Otherwise, the copy of the value in the Items collection won''t have
changed at all.

That''s why you''d be allowed to do it if IconItem were a reference type
- in that case, the change you''d expect would indeed have been made.

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


I think i would declare IconItem as a class, not as a struct.
Because putting "b.Items [e.Index] = ii;"
causes the item to be redrawn, thus resulting in a inifinite loop.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Bern <x@x.com> wrote:


the following code produces an error:
"The left-hand side of an assignment must be a variable, property or
indexer"

-----------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

((IconItem) b.Items [e.Index]).Bounds = new Rectangle (0, 0, 1, 1);
}
----------------------------------------------------------------------

But the following does not:

----------------------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

IconItem ii = ((IconItem) b.Items [e.Index]);
ii.Bounds = new Rectangle (0, 0, 1, 1);
}

----------------------------------------------------------------------



It''s hard to know for absolutely sure without knowing what IconItem is,
but my guess is that it''s a value type. In that case, you''re running
into the following from section 14.13.1 of the C# spec:

<quote>
When a property or indexer declared in a struct-type is the target of
an assignment, the instance expression associated with the property or
indexer access must be classified as a variable. If the instance
expression is classified as a value, a compile-time error occurs.
</quote>

The problem is that if it *did* compile, it would do what the second
bit of code does - which is totally useless. You need this at the end
of your method:

b.Items[e.Index] = ii;

Otherwise, the copy of the value in the Items collection won''t have
changed at all.

That''s why you''d be allowed to do it if IconItem were a reference type
- in that case, the change you''d expect would indeed have been made.

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



I can''t make it fail if I create the following code:

class App
{
static void Blah(object sender, DrawItemEventArgs e)
{
ListBox b = sender as ListBox;
IconItem ii = ((IconItem) b.Items [e.Index]);
ii.Bounds = new Rectangle (0, 0, 1, 1);
}
}

class IconItem
{
public Rectangle Bounds;
}
}

So can you produce a concise snippet that shows your behavior?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

the following code produces an error:
"The left-hand side of an assignment must be a variable, property or
indexer"

-----------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

((IconItem) b.Items [e.Index]).Bounds = new Rectangle (0, 0, 1, 1);
}
----------------------------------------------------------------------

But the following does not:

----------------------------------------------------------------------

private void ListBox_DrawItem (object sender, DrawItemEventArgs e){

ListBox b = sender as ListBox;

IconItem ii = ((IconItem) b.Items [e.Index]);
ii.Bounds = new Rectangle (0, 0, 1, 1);
}

----------------------------------------------------------------------
Why??


这篇关于为什么会有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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