Is there a way to cast a user control as a specific user control so I have access to it's public properties? Basicly I'm foreaching through a placeholder's controls collection and I'm trying to access the user control's public properties.
foreach(UserControl uc in plhMediaBuys.Controls)
{
uc.PulblicPropertyIWantAccessTo;
}
foreach(UserControl uc in plhMediaBuys.Controls) {
MyControl c = uc as MyControl;
if (c != null) {
c.PublicPropertyIWantAccessTo;
}
}
foreach(UserControl uc in plhMediaBuys.Controls)
{
if (uc is MySpecificType)
{
return (uc as MySpecificType).PulblicPropertyIWantAccessTo;
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With