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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 262k
  • Answers 262k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have been working with WPF since the beta versions… May 13, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer Important: if you only need the one endian, see the… May 13, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer Here you go: ini_get_all() or get_loaded_extensions() were the closest I… May 13, 2026 at 11:54 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
In order to apply a triggered animation to all ToolTip s in my app,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.