3

I want to change the property of an existing CheckBox object in my code:

string checkBoxName = "checkBox" + Convert.ToString(index);    
Object objCheckBox = checkBoxName;    
CheckBox myCheckBox = objCheckBox as CheckBox;

if (words[1] == "-1")
{
    myCheckBox.Checked = false;
}
else
{
    myCheckBox.Checked = true;
}

The code snippet above did not cause a compile error but caused a runtime error as

Object reference not set to an instance of an object.

What should I do?

4
  • You are saving a string in objCheckBox and trying to convert that as CheckBox. That obviously won't work. Commented May 23, 2013 at 12:34
  • @Nolonar If it was that obvious, the OP wouldn't have tried it that way, would he? Commented May 23, 2013 at 12:35
  • I agree, it was a stupid approach :D. Commented May 23, 2013 at 12:36
  • @ThorstenDittmar Of course he would have. Lack of experience/knowledge/focus. I usually do some really stupid and obvious mistakes myself, spend hours trying to resolve the problem, then curse myself for not noticing the obvious sooner. Commented May 23, 2013 at 12:37

2 Answers 2

7

I'm assuming that you're talking about WinForms

You can not do it that way. The following line assigns null to myCheckBox, as objCheckBox is of type string and not of type CheckBox.

CheckBox myCheckBox = objCheckBox as CheckBox;

What you need to do is iterate all controls on the form to find the control named checkBoxName. You can do it via LINQ, or you can do it like this:

Control[] controls = this.Find(checkBoxName, true);
if (controls != null && controls.Length > 0)
{
    (controls[0] as CheckBox).Checked = words[1] != "-1";
}

A LINQ approach could look like this:

Control c = (from Control c in this.Controls where c.Name.Equals(checkBoxName) select c).FirstOrDefault();
if (c != null)
{
    ....
}

Please note that if the CheckBox is not a direct child of the form itself, the LINQ approach won't find it. To make sure it is always found, you'll need to recursively also search container controls - for example: If you find a control which is a Panel, you need to also search the panel's children.


I edited my code according to the comments - just remembered the Find method, too.

Sign up to request clarification or add additional context in comments.

1 Comment

The Control Collection has a Find Method that does (more or less) what the first code does. And it does it recursively.
6

The code snippet you have shown yields that exception because of the line

CheckBox myCheckBox = objCheckBox as CheckBox;

The as operator tries to cast the left operand into the type specified in the right operand. If the cast fails, the result will be null.

In your case, objCheckBox is a string. string cannot be converted into CheckBox, hence the result that gets saved in myCheckBox is null.


Instead, use one of the following methods:

  • Store all of your check boxes in an array. You will then be able to access the elements in that array directly with their index (which you seem to have stored in the index variable).
  • Depending on which UI toolkit you are using, there should be a method to find a control by its name. Pass the complete check box name to that method, and it will return the appropriate CheckBox instance (if found).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.