Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:42:33+00:00 2026-05-11T16:42:33+00:00

I’m not really sure how to ask this question. Suppose I have a class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:42:34+00:00Added an answer on May 11, 2026 at 4:42 pm

    It’s not a stupid question, it’s a good one. 🙂

    What you’re asking for on the interface is commonly referred to as “duck-typing.” It isn’t supported right now, but C#4.0 will support it via the new “dynamic” keyword.

    You’ve really got three choices that I’m aware of at this point:

    1. You can go up the tree until you find the common ancestor (probably Component) and then downcast to your supported types. If the downcast fails, you throw or handle appropriately.

      Pro: Minimal code duplication.

      Con: You’re trading away compile-time type safety for runtime type safety. You have to add error checking/handling for invalid casts.

      Code:

      public void UseThumbnail(Component c) 
      {
          PictureBox p = c as PictureBox;
          if(p != null) // do whatever
          // so forth
      }
      
    2. You can duplicate functionality as appropriate for everything that you need to implement this functionality for.

      Pro: Maintain compile time type safety

      Con: You’re duplicating code for different types. This can grow into a significant maintenance burden, especially if you’re handling more than two similar classes.

      Code:

      public void UsePictureBox(PictureBox p)
      {
          // Some code X
      }
      
      public void UseOtherControl(OtherControl p)
      {
          // Some code X
      }
      
    3. You can create your special interface and subclass the classes you want to support to expose that common functionality.

      Pro: You get compile time safety and can program against your new interface.

      Con: You have to add an empty subclass for everything you’re dealing with, and you need to use them.

      Code:

      public class ThumbnailPictureBox : PictureBox, IThumbnail 
      { }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.