Newbie Question regarding checkboxes

  • Thread starter Thread starter Steve Wasser
  • Start date Start date
S

Steve Wasser

I'm doing c# code behind, and am getting the error: CS1041: Identifier
expected, 'checked' is a keyword with the following code:

public void Clicky(object sender, EventArgs e)
{
if (sender.checked){
BOL.Visible = true;
BOL.Text="";
}else{
BOL.visible=false;
}

I'm making the hop from VB.NET to C#, so I'm sure my syntax or sender
reference could be wrong.

Thanks,
 
Steve Wasser said:
I'm doing c# code behind, and am getting the error: CS1041: Identifier
expected, 'checked' is a keyword with the following code:
if (sender.checked)

Bit of a C# newbie myself but I believe this method may be correct, no doubt
someone will comment.

//------------------------------------------------
CheckBox myCheckBox = sender as CheckBox;

if(myCheckBox != null)
{
if(myCheckBox.Checked == true)
{
// code in here
}
}
//------------------------------------------------
 
"sender" is defined as an object, which does not have a "checked" property.
If you want to treat it like a specific type of object you would have to
cast it like

((SomeClass)sender).Checked

Assuming SomeClass has a property called Checked (which is different than
lower-case "checked").

This is equivalent to CType in VB.Net as far as I remember.

"checked" is a reserved word in C# that means the following code should be
checked for overruns, for example:

checked
{
byte b = (byte)255;
b++; // Would just become 0 again if not for the checked keyword.
}

Eric
 
Back
Top