The mistake is exactly what is written, you can’t do what you did. The solution is to create a normal variable and then assign its value to the property you want.
A variable passed by out
can only be a true variable. A property is not a variable, it seems to be a variable, but it is a method that is called to possibly access a variable indirectly. The modifier out
, as well as ref
, end up having a indirect and so they are passed as a pointer and the expected semantics is that this is a pointer to a given directly. If you have a new indirect compiler doesn’t know what to do.
If you want to understand more about how this modifier works has answer on What are the out and ref parameters. And to learn more about property has more on How the properties in C work#? (plus and plus). I strongly recommend reading these and others links that give insight into the mechanisms to understand what you’re doing and not just follow cake recipes.
Then it would be something like this:
if (!decimal.TryParse(ReadLine(), out var valor)) return 1;
produtos[i].Preco = valor;
I put in the Github for future reference.
that can’t
out produtos[i].Preco
you need to pass a variable that cannot be a property ... quick solutionif (decimal.TryParse(ReadLine(), out var p)) { produtos[i].Preco = p; }
If you passed the Tryparse test you pass the value for the property ... It lacks context, so I didn’t answer, doesn’t it make sense to put a Return 1 there ?? where will return 1 ... unknown problems in your code, so I asked for closure for the lack of a context to clarify all the code that was not posted.– novic