2
Answers

Casting objects

Daniel

Daniel

17y
2.1k
1
I am attempting to make two check boxes mutually exclusive. When one is checked I will read the name of one and create an instance of the other to uncheck/check it. I am naming all my check boxes: _YES _NO so when I get the name of the of the control, by looking for the substring up to the "-" in the control name, i can then concatenate the opposite control name (i.e + "_NO"). My question is once i have the opposite control name as a string, how do I cast that into the check box object I want on the form? Desperate for help. Pseudo code below. Thanks,

psuedo code:
//cast the object into a checkbox
CheckBox chkbox = (CheckBox)sender;
string sCtrlName = chkbox.Name;
//if str is greater than -1,
// then string was found
if (sCtrlName.IndexOf("YES") > -1 )
{
//since YES is selected, uncheck "NO"
int endPos = sCtrlName.IndexOf("_");
string sName = sCtrlName.Substring(0, endPos);
sName += "_NO";
//create the opposite checkbox control
// if c_YES, then create cbo_NO and vice versa
CheckBox chkTemp = new CheckBox();

/// This is DOES NOT WORK
cboTemp = (CheckBox)sName;
else
// insert code for "NO"
{

}

Answers (2)