✅ Array Assignment Error

I'm iterating over a list of classes, and in each class, there's a property that is an array which I iterate over and remove anything that matches what I want to remove. Originally, I did a .Where() for comparison and then .ToList() which I iterated over and did a Array.Clear on the original array. But that just leaves behind null classes in the Array. So I changed my approach, converted the array to a list and did a remove all. I want to set the original Array equal to this List<T>.ToArray(), but when I try that, I get "the Left-hand side of an assignment must be a variable, property or indexer" error.
foreach (Foo foo in fooBar ?? Enumberable.Empty<Foo>)
{
List<Bar>? barList = foo?.bar.ToList<Bar>();
barList?.RemoveAll(bar => bar.Name == "");
foo?.bar = barList??.ToArray(); // error here
}
foreach (Foo foo in fooBar ?? Enumberable.Empty<Foo>)
{
List<Bar>? barList = foo?.bar.ToList<Bar>();
barList?.RemoveAll(bar => bar.Name == "");
foo?.bar = barList??.ToArray(); // error here
}
7 Replies
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
therealgooey
therealgooeyOP2y ago
Wondering if anyone can take a look at this.
TheRanger
TheRanger2y ago
what does the error message say?
therealgooey
therealgooeyOP2y ago
CS0131: The left-hand side of an assignment must be a variable, property or indexer.
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Failure
int? i = 2;
i?.Value = 3;
int? i = 2;
i?.Value = 3;
Exception: CompilationErrorException
- 'method group' cannot be made nullable.
- 'method group' cannot be made nullable.
Compile: 526.995ms | Execution: 0.000ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
yea i dont think foo?.bar works try instead
if (foo != null)
{
foo.bar = yourValue;
}
if (foo != null)
{
foo.bar = yourValue;
}
therealgooey
therealgooeyOP2y ago
Yep, that was it! Now that I think about it, that makes sense. I appreciate the help
Want results from more Discord servers?
Add your server