mardi 31 août 2021

Ternary operator error: potential misuse of operator?

I'm trying to use a ternary operator and am still learning so I'm not entirely familiar with how they do what they do.

I have a line of code that is as follows:

c.GetType() != typeof(CheckBox)
? c.Text = settingValue 
: ((CheckBox)c).Checked = bool.Parse(settingValue);

The purpose of this line of code is to test if the control c is of type CheckBox and then either set the text of the control or change the Checked state of the control. But this line gives me a CS0201 error in VS.

I know I could alternatively use an If statement for this but I wanted to see if I could condense it into one line with a ternary operator. What am I missing?

Aucun commentaire:

Enregistrer un commentaire