C
C#3mo ago
Cubic

is there any way to make something be checked for every time a field is updated or do i need to use

the property Energy is based on the field CurrentEnergy. CurrentEnergy is the one that keeps being set, and energy is only computed every now and then. any way to link this check to every update of CurrentEnergy? [this is all inside a class]
No description
7 Replies
Cubic
Cubic3mo ago
link between the two, if necessary
No description
Buddy
Buddy3mo ago
use a setter That maybe calls an event
private string _value;
public string Value
{
get => _value;
set
{
if (value == _value) return;
_value = value;
OnValueUpdated(value);
}
}
private string _value;
public string Value
{
get => _value;
set
{
if (value == _value) return;
_value = value;
OnValueUpdated(value);
}
}
Something like that
leowest
leowest3mo ago
that is also similar to what UI frameworks do
Cubic
Cubic3mo ago
that's definitely very useful to know about thanks 👍 honestly at the end of the day this still doesn't work because i need to add to the output string, which is only declared in certain scopes, so i wound up modifying some other properties to make this work
br4kejet
br4kejet3mo ago
You should probably check the new value of CurrentEnergy before actually updating it. Setting a value in a property getter is typically a nono In the same CurrentEnergy setter, you can then check if the new value equals the max energy, and if so, increment output by Phase3()
Cubic
Cubic3mo ago
honestly, yeah, that's fair, and i did try changing that for a few other properties that had the same problem, but i then realised some of my code is based on the asusmption that those values are not updated until checked.... so yeah i'm probably gonna need to rework my code soon lmao for now, i'd rather have it this way than have a strange and unclear discrepancy between when to update Energy vs CurrentEnergy or something like that.