foreach(UserControl uc in plhMediaBuys.Controls)
{
if (uc is MySpecificType)
{
return (uc as MySpecificType).PulblicPropertyIWantAccessTo;
}
}
Kon
2008-10-22 19:00:35
foreach(UserControl uc in plhMediaBuys.Controls)
{
if (uc is MySpecificType)
{
return (uc as MySpecificType).PulblicPropertyIWantAccessTo;
}
}
foreach(UserControl uc in plhMediaBuys.Controls) {
MyControl c = uc as MyControl;
if (c != null) {
c.PublicPropertyIWantAccessTo;
}
}
I prefer to use:
foreach(UserControl uc in plhMediaBuys.Controls)
{
ParticularUCType myControl = uc as ParticularUCType;
if (myControl != null)
{
// do stuff with myControl.PulblicPropertyIWantAccessTo;
}
}
Mainly because using the is keyword causes two (quasi-expensive) casts:
if( uc is ParticularUCType ) // one cast to test if it is the type
{
ParticularUCType myControl = (ParticularUCType)uc; // second cast
ParticularUCType myControl = uc as ParticularUCType; // same deal this way
// do stuff with myControl.PulblicPropertyIWantAccessTo;
}