❔ 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
TheRanger2y ago
u have a List<object> or something?
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
(1) I create an empty object in a List. (2) I want to update a property in that empty object.
TheRanger
TheRanger2y ago
@Lucas.mrozikpl
Lucas.mrozikpl
Lucas.mrozikplOP2y 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
TheRanger2y ago
lets say u want to choose the first object
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
yes
TheRanger
TheRanger2y ago
ActivitiesPerDay[0].YourProperty = YourValue;
ActivitiesPerDay[0].YourProperty = YourValue;
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
was that simple? 🙂
TheRanger
TheRanger2y ago
yes, its called Indexer
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
I was looking into some LINQ methods but I could not find anything useful
TheRanger
TheRanger2y ago
ActivitiesPerDay.First().YourProperty = YourValue;
ActivitiesPerDay.First().YourProperty = YourValue;
First is a LINQ Method
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
let me use it then! Thank you!
TheRanger
TheRanger2y ago
i mean u can use indexers as it can be slower with LINQ
Lucas.mrozikpl
Lucas.mrozikplOP2y ago
ahh I see
Anton
Anton2y ago
it will only work with reference types (list indexer is not a ref getter, right?)
TheRanger
TheRanger2y ago
?
Anton
Anton2y ago
well if it's a get set property, it won't work with structs if it's a ref get, it will
TheRanger
TheRanger2y ago
what exactly wont work with structs?
Anton
Anton2y ago
the indexer will make a copy
TheRanger
TheRanger2y ago
copy of what?
Anton
Anton2y ago
hence the item in the array won't be modified
TheRanger
TheRanger2y ago
no it wont
Anton
Anton2y ago
copy of the item in the array yes it will
TheRanger
TheRanger2y ago
it copies if u try to assign it to a variable but here im directly accessing the value
Anton
Anton2y ago
how do I run code here? i forgot
TheRanger
TheRanger2y ago
$eval
MODiX
MODiX2y 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
MODiX2y 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
MODiX2y 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
Anton2y 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
MODiX2y 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
MODiX2y 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
Anton2y ago
same here
TheRanger
TheRanger2y ago
u said array here
Anton
Anton2y ago
sorry I meant list array indexer is ref get
MODiX
MODiX2y 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
Anton2y ago
so like this
TheRanger
TheRanger2y ago
i dont think array has an indexer, its just works directly
Anton
Anton2y 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
TheRanger2y 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
Anton2y ago
well yeah this one gets you a copy of the value
TheRanger
TheRanger2y ago
why would array's getter be ref anyway
Anton
Anton2y ago
wdym?
TheRanger
TheRanger2y ago
.
Anton
Anton2y ago
wdym why?
TheRanger
TheRanger2y ago
its just a getter whats the point of it to have ref
MODiX
MODiX2y 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
TheRanger2y ago
in that code u added ref manually
Anton
Anton2y ago
so what that's the same with custom ref indexers
TheRanger
TheRanger2y ago
doesnt mean array's getter is ref
Anton
Anton2y ago
to assign to a ref variable you need to type ref both for the variable type and for the expression
TheRanger
TheRanger2y ago
just like how ref is placed here
Anton
Anton2y ago
well if it weren't ref you couldn't assign like this ref var item = ref a[index] but you can
TheRanger
TheRanger2y 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
Anton2y 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
TheRanger2y ago
the ref var i = ref a[0]; trick?
Anton
Anton2y ago
yeah
TheRanger
TheRanger2y ago
let me test
Anton
Anton2y ago
It's C# 8 semantics i think
TheRanger
TheRanger2y ago
well T[] i cant find it in source code probably magic, lol
Anton
Anton2y 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
TheRanger2y ago
this sadens me
MODiX
MODiX2y 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
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.
Want results from more Discord servers?
Add your server