❔ Updating empty object in a list
Hi, is there any way to update specific empty property (e.g. of int type) of an object in a List?
64 Replies
u have a List<object> or something?
(1) I create an empty object in a List. (2) I want to update a property in that empty object.
@Lucas.mrozikpl
in my class I have:
public ObservableCollection<Day> ActivitiesPerDay { get; set; } = new();
in a method somewhere I created an empty object there
ActivitiesPerDay.Add(
new Day()
{
});
now I want to update chosen object's property
if the object had filled property I would have updated it easily
lets say u want to choose the first object
yes
was that simple? 🙂
yes, its called Indexer
I was looking into some LINQ methods but I could not find anything useful
First is a LINQ Method
let me use it then! Thank you!
i mean u can use indexers
as it can be slower with LINQ
ahh I see
it will only work with reference types (list indexer is not a ref getter, right?)
?
well if it's a get set property, it won't work with structs
if it's a ref get, it will
what exactly wont work with structs?
the indexer will make a copy
copy of what?
hence the item in the array won't be modified
no it wont
copy of the item in the array
yes it will
it copies if u try to assign it to a variable
but here im directly accessing the value
how do I run code here? i forgot
$eval
To compile C# code in Discord, use
!eval
Example:
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
To compile C# code in Discord, use !eval
Example:
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!AntonC#3545
REPL Result: Failure
Exception: CompilationErrorException
Compile: 582.411ms | Execution: 0.000ms | React with ❌ to remove this embed.
AntonC#3545
REPL Result: Failure
Exception: CompilationErrorException
Compile: 544.023ms | Execution: 0.000ms | React with ❌ to remove this embed.
see, it doesn't even work with a simple get set property
it will work with ref get
what's different about lists?
AntonC#3545
REPL Result: Failure
Exception: CompilationErrorException
Compile: 563.141ms | Execution: 0.000ms | React with ❌ to remove this embed.
AntonC#3545
REPL Result: Failure
Exception: CompilationErrorException
Compile: 605.340ms | Execution: 0.000ms | React with ❌ to remove this embed.
same here
u said array here
sorry I meant list
array indexer is ref get
AntonC#3545
REPL Result: Success
Result: int
Compile: 552.809ms | Execution: 49.238ms | React with ❌ to remove this embed.
so like this
i dont think array has an indexer, its just works directly
well it does do bounds checking
and it's a class
so idk about that
even if it's some internal special thing, it acts just like a ref indexer
in class Array atleast
no ref here
i cant find T[] class, probably a built in thing
well yeah this one gets you a copy of the value
why would array's getter be ref anyway
wdym?
.
wdym why?
its just a getter
whats the point of it to have
ref
AntonC#3545
REPL Result: Failure
Exception: CompilationErrorException
Compile: 577.046ms | Execution: 0.000ms | React with ❌ to remove this embed.
in that code u added
ref
manuallyso what
that's the same with custom ref indexers
doesnt mean array's getter is ref
to assign to a ref variable you need to type ref both for the variable type and for the expression
just like how ref is placed here
well if it weren't ref you couldn't assign like this
ref var item = ref a[index]
but you canthis is arraysegment atleast
no ref here
That means you can't do this trick with array segment
you can't assign to a ref variable if the indexer is not ref
the
ref var i = ref a[0];
trick?yeah
let me test
It's C# 8 semantics i think
well T[] i cant find it in source code
probably magic, lol
yeah it's got to be some internal thing
but the fact is a fact
that the indexer acts like a ref get
this sadens me
TheRanger#3357
REPL Result: Failure
Exception: CompilationErrorException
Compile: 570.442ms | Execution: 0.000ms | React with ❌ to remove this embed.
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.