开发者

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

开发者 https://www.devze.com 2023-02-03 19:19 出处:网络
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.

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


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消