why is there an error?

  • Thread starter Thread starter Bern
  • Start date Start date
B

Bern

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);
}
 
Bern said:
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.
 
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 said:
Bern said:
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.
 
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);
}
 
Bern said:
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.

Certainly using reference types tends to be simpler in .NET. I find it
very rarely a good idea to declare my own value types.
 
Richard Blewett said:
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?

Just change your IconItem class to a struct and you'll see the problem.
 
Nope, that compiles cleanly,

its now a meaningless piece of code in Blah but it compiles fine. Thats why I wanted the OP to post code that actually showed the problem

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

Richard Blewett said:
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?

Just change your IconItem class to a struct and you'll see the problem.
 
Richard Blewett said:
Nope, that compiles cleanly,

its now a meaningless piece of code in Blah but it compiles fine.
Thats why I wanted the OP to post code that actually showed the
problem

Whoops, sorry - I didn't read your code carefully. Your code is the
equivalent of the *second* piece of code the OP posted, which compiles
without problem as he stated. If you change your code to match the
first section in his original post, you'll see the error.
 
Doh! Its me who should be reading more carefully - I thought he said the ifrst compiled but the second did not

Regards

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

Richard Blewett said:
Nope, that compiles cleanly,

its now a meaningless piece of code in Blah but it compiles fine.
Thats why I wanted the OP to post code that actually showed the
problem

Whoops, sorry - I didn't read your code carefully. Your code is the
equivalent of the *second* piece of code the OP posted, which compiles
without problem as he stated. If you change your code to match the
first section in his original post, you'll see the error.
 
Hi Bern,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top