Reputation: 119
The original user control code in the accpected answer in this link : How to animate dots in UserControl Paint event?
i changed it a bit and added and option to save the changes. so i added some user settings properties one of them is SaveState :
[DefaultValue(false)]
public bool SaveState
{
get => saveState;
set
{
if (value)
{
saveState = value;
}
Properties.Settings.Default.SaveState = value;
Properties.Settings.Default.Save();
}
}
and in the constructor :
public LoadingLabel()
{
InitializeComponent();
SaveState = Properties.Settings.Default.SaveState;
}
The problem is when i;m adding another line to update the SaveState the whole application and visual studio crash the visual studio start over again without the control being dragged to form1 designer. the crash happens after i made the changes and when dragging the control to the form1 desginer.
This is what i changed :
[DefaultValue(false)]
public bool SaveState
{
get => saveState;
set
{
if (value)
{
saveState = value;
SaveState = value;
}
I added the line :
SaveState = value;
because it's updating the saveState and working fine but visually i don't see the property SaveState changing :
The SaveState is set to true but when i'm changing it to false i will see it false only if i will remove and drag the control over again to the designer.
not sure why it's crashing when trying to updating the SaveState it self.
Upvotes: 3
Views: 145
Reputation: 125749
When you write
SaveState = value;
you call the setter.
When you do so in the setter's code, this causes infinite recursion - you assign to SaveState
in the setter, which calls the setter, which assigns to SaveState
, which calls the setter, which assigns to SaveState
, which calls the setter, and so forth forever, which is causing the IDE to crash.
The moral of the story: NEVER assign to the property inside the setter; ALWAYS assign to the backing field instead.
Upvotes: 2