❔ 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
TheRanger
TheRanger17mo ago
u have a List<object> or something?
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
(1) I create an empty object in a List. (2) I want to update a property in that empty object.
TheRanger
TheRanger17mo ago
@Lucas.mrozikpl
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
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
TheRanger
TheRanger17mo ago
lets say u want to choose the first object
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
yes
TheRanger
TheRanger17mo ago
ActivitiesPerDay[0].YourProperty = YourValue;
ActivitiesPerDay[0].YourProperty = YourValue;
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
was that simple? 🙂
TheRanger
TheRanger17mo ago
yes, its called Indexer
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
I was looking into some LINQ methods but I could not find anything useful
TheRanger
TheRanger17mo ago
ActivitiesPerDay.First().YourProperty = YourValue;
ActivitiesPerDay.First().YourProperty = YourValue;
First is a LINQ Method
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
let me use it then! Thank you!
TheRanger
TheRanger17mo ago
i mean u can use indexers as it can be slower with LINQ
Lucas.mrozikpl
Lucas.mrozikpl17mo ago
ahh I see
Anton
Anton17mo ago
it will only work with reference types (list indexer is not a ref getter, right?)
TheRanger
TheRanger17mo ago
?
Anton
Anton17mo ago
well if it's a get set property, it won't work with structs if it's a ref get, it will
TheRanger
TheRanger17mo ago
what exactly wont work with structs?
Anton
Anton17mo ago
the indexer will make a copy
TheRanger
TheRanger17mo ago
copy of what?
Anton
Anton17mo ago
hence the item in the array won't be modified
TheRanger
TheRanger17mo ago
no it wont
Anton
Anton17mo ago
copy of the item in the array yes it will
TheRanger
TheRanger17mo ago
it copies if u try to assign it to a variable but here im directly accessing the value
Anton
Anton17mo ago
how do I run code here? i forgot
TheRanger
TheRanger17mo ago
$eval
MODiX
MODiX17mo ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
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:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Failure
$eval
struct Item
{
public int value;
}
class A
{
Item item;
public Item this[int index]
{
get => item;
set => item = value;
}
}

A a = new A();
a[0].value = 5;
a[0].value
$eval
struct Item
{
public int value;
}
class A
{
Item item;
public Item this[int index]
{
get => item;
set => item = value;
}
}

A a = new A();
a[0].value = 5;
a[0].value
Exception: CompilationErrorException
- Unexpected character '$'
- ; expected
- The name 'eval' does not exist in the current context
- Cannot modify the return value of 'A.this[int]' because it is not a variable
- Unexpected character '$'
- ; expected
- The name 'eval' does not exist in the current context
- Cannot modify the return value of 'A.this[int]' because it is not a variable
Compile: 582.411ms | Execution: 0.000ms | React with ❌ to remove this embed.
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Failure
struct Item
{
public int value;
}
class A
{
Item item;
public Item this[int index]
{
get => item;
set => item = value;
}
}

A a = new A();
a[0].value = 5;
a[0].value
struct Item
{
public int value;
}
class A
{
Item item;
public Item this[int index]
{
get => item;
set => item = value;
}
}

A a = new A();
a[0].value = 5;
a[0].value
Exception: CompilationErrorException
- Cannot modify the return value of 'A.this[int]' because it is not a variable
- Cannot modify the return value of 'A.this[int]' because it is not a variable
Compile: 544.023ms | Execution: 0.000ms | React with ❌ to remove this embed.
Anton
Anton17mo ago
see, it doesn't even work with a simple get set property it will work with ref get what's different about lists?
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Failure
public struct A
{
public int value;
}
var l = new List<A>();
l.Add(new A());
a[0].value = 5;
a[0].value
public struct A
{
public int value;
}
var l = new List<A>();
l.Add(new A());
a[0].value = 5;
a[0].value
Exception: CompilationErrorException
- The name 'a' does not exist in the current context
- The name 'a' does not exist in the current context
- The name 'a' does not exist in the current context
- The name 'a' does not exist in the current context
Compile: 563.141ms | Execution: 0.000ms | React with ❌ to remove this embed.
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Failure
public struct A
{
public int value;
}
var l = new List<A>();
l.Add(new A());
l[0].value = 5;
l[0].value
public struct A
{
public int value;
}
var l = new List<A>();
l.Add(new A());
l[0].value = 5;
l[0].value
Exception: CompilationErrorException
- Cannot modify the return value of 'List<A>.this[int]' because it is not a variable
- Cannot modify the return value of 'List<A>.this[int]' because it is not a variable
Compile: 605.340ms | Execution: 0.000ms | React with ❌ to remove this embed.
Anton
Anton17mo ago
same here
TheRanger
TheRanger17mo ago
u said array here
Anton
Anton17mo ago
sorry I meant list array indexer is ref get
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Success
struct Item
{
public int value;
}
class A
{
Item item;
public ref Item this[int index]
{
get => ref item;
}
}

A a = new A();
a[0].value = 5;
a[0].value
struct Item
{
public int value;
}
class A
{
Item item;
public ref Item this[int index]
{
get => ref item;
}
}

A a = new A();
a[0].value = 5;
a[0].value
Result: int
5
5
Compile: 552.809ms | Execution: 49.238ms | React with ❌ to remove this embed.
Anton
Anton17mo ago
so like this
TheRanger
TheRanger17mo ago
i dont think array has an indexer, its just works directly
Anton
Anton17mo ago
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
TheRanger
TheRanger17mo ago
public object? GetValue(int index)
{
if (Rank != 1)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray);

return InternalGetValue(GetFlattenedIndex(index));
}
public object? GetValue(int index)
{
if (Rank != 1)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray);

return InternalGetValue(GetFlattenedIndex(index));
}
in class Array atleast no ref here i cant find T[] class, probably a built in thing
Anton
Anton17mo ago
well yeah this one gets you a copy of the value
TheRanger
TheRanger17mo ago
why would array's getter be ref anyway
Anton
Anton17mo ago
wdym?
TheRanger
TheRanger17mo ago
.
Anton
Anton17mo ago
wdym why?
TheRanger
TheRanger17mo ago
its just a getter whats the point of it to have ref
MODiX
MODiX17mo ago
AntonC#3545
REPL Result: Failure
struct Item
{
public int value;
}

Item[] a = new Item[1];
ref var i = ref a[0];
i.value = 1;
a[0]
struct Item
{
public int value;
}

Item[] a = new Item[1];
ref var i = ref a[0];
i.value = 1;
a[0]
Exception: CompilationErrorException
- Syntax error, '(' expected
- Identifier expected
- Identifier expected
- ) expected
- The type or namespace name 'a' could not be found (are you missing a using directive or an assembly reference?)
- Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
- The contextual keyword 'var' may only appear within a local variable declaration or in script code
- 'i(ref a[])' must declare a body because it is not marked abstract, extern, or partial
- 'i(ref a[])' is a method, which is not valid in the given context
- Syntax error, '(' expected
- Identifier expected
- Identifier expected
- ) expected
- The type or namespace name 'a' could not be found (are you missing a using directive or an assembly reference?)
- Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
- The contextual keyword 'var' may only appear within a local variable declaration or in script code
- 'i(ref a[])' must declare a body because it is not marked abstract, extern, or partial
- 'i(ref a[])' is a method, which is not valid in the given context
Compile: 577.046ms | Execution: 0.000ms | React with ❌ to remove this embed.
TheRanger
TheRanger17mo ago
in that code u added ref manually
Anton
Anton17mo ago
so what that's the same with custom ref indexers
TheRanger
TheRanger17mo ago
doesnt mean array's getter is ref
Anton
Anton17mo ago
to assign to a ref variable you need to type ref both for the variable type and for the expression
TheRanger
TheRanger17mo ago
just like how ref is placed here
Anton
Anton17mo ago
well if it weren't ref you couldn't assign like this ref var item = ref a[index] but you can
TheRanger
TheRanger17mo ago
this is arraysegment atleast
public T this[int index]
{
get
{
if ((uint)index >= (uint)_count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

return _array![_offset + index];
}
set
{
if ((uint)index >= (uint)_count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

_array![_offset + index] = value;
}
}
public T this[int index]
{
get
{
if ((uint)index >= (uint)_count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

return _array![_offset + index];
}
set
{
if ((uint)index >= (uint)_count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

_array![_offset + index] = value;
}
}
no ref here
Anton
Anton17mo ago
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
TheRanger
TheRanger17mo ago
the ref var i = ref a[0]; trick?
Anton
Anton17mo ago
yeah
TheRanger
TheRanger17mo ago
let me test
Anton
Anton17mo ago
It's C# 8 semantics i think
TheRanger
TheRanger17mo ago
well T[] i cant find it in source code probably magic, lol
Anton
Anton17mo ago
yeah it's got to be some internal thing but the fact is a fact that the indexer acts like a ref get
TheRanger
TheRanger17mo ago
this sadens me
MODiX
MODiX17mo ago
TheRanger#3357
REPL Result: Failure
Array array = new int[1];
Console.WriteLine(array[0]);
Array array = new int[1];
Console.WriteLine(array[0]);
Exception: CompilationErrorException
- Cannot apply indexing with [] to an expression of type 'Array'
- Cannot apply indexing with [] to an expression of type 'Array'
Compile: 570.442ms | Execution: 0.000ms | React with ❌ to remove this embed.
Accord
Accord17mo 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.