You cannot assign a property or indexer to 'System.Nullable.Value' - it is read-only

I am developing a winform application. Based on some values ​​(say x), I want to show the user a warning, the timer has updated another value (y) that affects x, and will check the x value and show the warning to the user. Alert displays a message box with yes / no options if the user clicks yes and then processes.

If the user has not responded to the warning for a long time (say, 10 minutes), several warning messages may appear, I want me to create the DialableResult variable with a value of zero, so I can check whether the user has selected any parameter or not. Now the problem is that it does not allow setting the value of this variable

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

I give me a mistake. The property or index "System.Nullable.Value" cannot be assigned - it is read-only

+4
source share
2 answers

The problem is that you are trying to directly assign the Value property. The Value property is marked as read-only, so the compiler shows you this error.

Instead, you should assign the value to the Nullable<T> variable just like any other type. For example, the code above will simply become:

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

The only thing that changes is access to the value. First you need to check the HasValue property, and if it returns True, then you will get the value using the Value property . If the HasValue property returns False, then the value of the object is undefined.

+7
source

For what it's worth, here you don't need a value with a zero value.

The DialogResult enumeration is None, which can be used to indicate that the user has not selected an option.

+1
source

Source: https://habr.com/ru/post/1335044/


All Articles