controls class

  • Thread starter Thread starter John Underwood
  • Start date Start date
J

John Underwood

Hi. In the following code i'm able to find all Label controls in the
controls
class, I'm looking for a cleaner way to look for control types (not a
specifice control).. I played around with the getType() but was not
able to figure out a way to do a test against a Label control.. Thanks
for any help..

John



for (int x=0;x<this.Controls.Count;x++)
{
if(this.Controls[x].GetType().Name.ToString().ToUpper() == "LABEL")
{
<snip>
}
}
 
I usually use something like the following:

for (int x=0;x<this.Controls.Count;x++)
{
Label lbl = this.Controls[x] as Label;
if (lbl != null)
{
// Do something
}
}

I remember reading somewhere that using the "as" operator as above gets rid
of one null-reference check vs. using "is" to see if Controls[x] is of type
Label and then converting it & assigning it to a variable of type Label.
 
Just as a point of efficiency, I leave variable creation outside of
'for' loops whenever possible. I also like using real words rather than VB
style short variable names:

Label label;
for (int x=0;x<this.Controls.Count;x++)
{
label = this.Controls[x] as Label;
if ( label != null )
{
// Do something
}
}

Chris A.R.

Mike Powell said:
I usually use something like the following:

for (int x=0;x<this.Controls.Count;x++)
{
Label lbl = this.Controls[x] as Label;
if (lbl != null)
{
// Do something
}
}

I remember reading somewhere that using the "as" operator as above gets rid
of one null-reference check vs. using "is" to see if Controls[x] is of type
Label and then converting it & assigning it to a variable of type Label.




John Underwood said:
Hi. In the following code i'm able to find all Label controls in the
controls
class, I'm looking for a cleaner way to look for control types (not a
specifice control).. I played around with the getType() but was not
able to figure out a way to do a test against a Label control.. Thanks
for any help..

John



for (int x=0;x<this.Controls.Count;x++)
{
if(this.Controls[x].GetType().Name.ToString().ToUpper() == "LABEL")
{
<snip>
}
}
 
Chris A. R. said:
Just as a point of efficiency, I leave variable creation outside of

I seriously doubt it's any quicker whatsoever. Afterall, that reference
variable is only one slot on the stack, it's not like the compiler is going
to repeatedly move the stack pointer, it's cleverer than that.

Micro-optimization for no reason, with no profile comparisons, is a bad
habit, imo.

'for' loops whenever possible. I also like using real words rather than VB
style short variable names:

Label label;
for (int x=0;x<this.Controls.Count;x++)
{
label = this.Controls[x] as Label;
if ( label != null )
{
// Do something
}
}

Chris A.R.

Mike Powell said:
I usually use something like the following:

for (int x=0;x<this.Controls.Count;x++)
{
Label lbl = this.Controls[x] as Label;
if (lbl != null)
{
// Do something
}
}

I remember reading somewhere that using the "as" operator as above gets rid
of one null-reference check vs. using "is" to see if Controls[x] is of type
Label and then converting it & assigning it to a variable of type Label.




John Underwood said:
Hi. In the following code i'm able to find all Label controls in the
controls
class, I'm looking for a cleaner way to look for control types (not a
specifice control).. I played around with the getType() but was not
able to figure out a way to do a test against a Label control.. Thanks
for any help..

John



for (int x=0;x<this.Controls.Count;x++)
{
if(this.Controls[x].GetType().Name.ToString().ToUpper() == "LABEL")
{
<snip>
}
}
 
Back
Top