Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property or indexer 'System.Nullable.Value' cannot be assigned to -- it is read only

I'm developing an winform app. Based of some values (say x) I want to show user an alert, a timer updated other value (y), which affect x, and check for value of x and show alert to user. Alert show a message box with yes/no options, if user click yes then some processing.

If user did not respond to alert for a long time (say 10 min), there can multiple alert messages show, I want to prevent that I created a nullable DialogResult variable, so I can check if user has selected any option or not. Now the problem is that it does not allow to set me the value of that variable

taskAlert.Value=MessageBox.Show(kMessage, appErrorTitle, MessageBoxButtons.YesNo);

I gives me error -Property or indexer 'System.Nullable.Value' cannot be assigned to -- it is read only

like image 259
Sharique Avatar asked Feb 25 '23 10:02

Sharique


1 Answers

The problem is that you're trying to assign directly to the Value property. The Value property is marked as read-only, which is why the compiler is showing you that error.

Instead, you should assign a value to a variable of type Nullable<T> the exact same way that you would any other type. For example, the above code would simply become:

taskAlert = MessageBox.Show(kMessage, appErrorTitle, MessageBoxButtons.YesNo);

The only thing that changes is accessing the value. You first need to check the HasValue property, and if it return True, then you'll retrieve the value using the Value property. If the HasValue property returns False, then the value of the object is undefined.

like image 156
Cody Gray Avatar answered Apr 28 '23 12:04

Cody Gray