Interfacing common functionality between controls-Collection of common programming errors
I’m not really sure how to ask this question. Suppose I have a class that needs to access certain properties of a Control (for example, Visible and Location). Perhaps I want to use the same class to access properties of another item that have the same name, but the class might not derive from Control. So I tried making an interface:
public interface IThumbnail {
bool Visible { get; set; }
int Height { get; set; }
int Width { get; set; }
Image Image { get; set; }
Point Location { get; set; }
event EventHandler Click;
}
Note that, for example, PictureBox happens to implement this interface. However, because the class definition does not say that it implements IThumbnail, I can’t cast PictureBoxes to IThumbnails–I get an InvalidCastException at runtime. But why can’t the CLR ‘figure out’ that PictureBox really does implement IThumbnail (it just doesn’t explicitly say it does).
Also, what should I do to handle this situation? I want a way to access some of the PictureBox’s properties without having my class know it’s modifying a PictureBox.
Thx, Sam
PS- I’m a newbie to interface programming, so I apologize if this is a stupid q.