Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A property, indexer or dynamic member access may not be passed as an out or ref parameter [duplicate]

Hello I'm having trouble figuring this out. I have these structs and classes.

struct Circle
{ ... }

class Painting
{
     List<Circle> circles;

     public List<Circle> circles
     {
          get { return circles; }
     }
}

I am trying to modify one of the circles inside the painting class from outside it, using this code:

MutatePosition(ref painting.Circles[mutationIndex], painting.Width, painting.Height);

This line is giving me a compiler error:

A property, indexer or dynamic member access may not be passed as an out or ref parameter

Why is this, and what can I do to solve it without changing my code too much?

like image 829
CantMutate Avatar asked Jan 27 '11 06:01

CantMutate


1 Answers

The error is pretty clear - you can't pass a property to a ref parameter of a method.

You need to make a temporary:

var circle = painting.Circles[mutationIndex];
MutatePosition(ref circle, painting.Width, painting.Height);
painting.Circles[mutationIndex] = circle;

That being said, mutable structs are often a bad idea. You might want to consider making this a class instead of a struct.

like image 158
Reed Copsey Avatar answered Sep 28 '22 09:09

Reed Copsey