Home > Net >  Control does not contain a definition for "xxx" when in an array of controls
Control does not contain a definition for "xxx" when in an array of controls

Time:11-24

I have an array of controls that I have created:

public static string[] ldcNames = new string[16] {"ledIndicator0", "ledIndicator1", "ledIndicator2", "ledIndicator3", "ledIndicator4", "ledIndicator5", "ledIndicator6", "ledIndicator7", "ledIndicator8", "ledIndicator9", "ledIndicator10", "ledIndicator11", "ledIndicator12", "ledIndicator13", "ledIndicator14", "ledIndicator15"};

In other arrays of General Winform controls that I have made, I can access the properties of the controls. The above Array is an array of LedIndicators from TA.Winforms.Controls package.

I access the other controls in this manner:

var lbl1 = this.Controls[Global.lblNames[rangeLabel]];` and `lbl1.Text = "xxxxx" 

This works perfectly. Attempting to do the same for the LedIndicator control results in the following error: (Where "Cadence" is underlined with a red squiggly line in the VS IDE

var ld1 = this.Controls[Global.ldcNames[rowNum]];
ld1.Cadence = CadencePattern.SteadyOn;

Error CS1061 'Control' does not contain a definition for 'Cadence' and no accessible extension method 'Cadence' accepting a first argument of type 'Control' could be found.

I am able to access the standard properties but not any of the, I don't know, extended or unique properties of this control.

Completely lost here and desperately need help. Thanks in advance.

CodePudding user response:

As others have stated, you need to CAST the control to the correct type, LedIndicator in your case:

LedIndicator ld1 = (LedIndicator)this.Controls[Global.ldcNames[rowNum]];
ld1.Cadence = CadencePattern.SteadyOn;

You can also use the as keyword:

LedIndicator ld1 = this.Controls[Global.ldcNames[rowNum]] as LedIndicator;
ld1.Cadence = CadencePattern.SteadyOn;
  • Related