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 113747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:45:04+00:00 2026-05-11T02:45:04+00:00

I have some logic, which defines and uses some user-defined types, like these: class

  • 0

I have some logic, which defines and uses some user-defined types, like these:

class Word {   System.Drawing.Font font; //a System type   string text; }  class Canvass {   System.Drawing.Graphics graphics; //another, related System type   ... and other data members ...   //a method whose implementation combines the two System types   internal void draw(Word word, Point point)   {     //make the System API call     graphics.DrawString(word.text, word.font, Brushes.Block, point);   } } 

The logic, after doing calculations with the types (e.g. to locate each Word instance), indirectly uses some System APIs, for example by invoking the Canvass.draw method.

I’d like to make this logic independent of the System.Drawing namespace: mostly, in order to help with unit testing (I think unit tests’ output would be easier to verify if the draw method were drawing to something other than a real System.Drawing.Graphics instance).

To eliminate the logic’s dependency on the System.Drawing namespace, I thought I’d declare some new interfaces to act as placeholders for the System.Drawing types, for example:

interface IMyFont { }  interface IMyGraphics {   void drawString(string text, IMyFont font, Point point); }  class Word {   IMyFont font; //no longer depends on System.Drawing.Font   string text; }  class Canvass {   IMyGraphics graphics;  //no longer depends on System.Drawing.Graphics   ... and other data ...    internal void draw(Word word, Point point)   {     //use interface method instead of making a direct System API call     graphics.drawText(word.text, word.font, point);   } } 

If I did this, then different assemblies could have different implementations of the IMyFont and IMyGraphics interface, for example …

class MyFont : IMyFont {   System.Drawing.Font theFont; }  class MyGraphics : IMyGraphics {   System.Drawing.Graphics theGraphics;    public void drawString(string text, IMyFont font, Point point)   {      //!!! downcast !!!      System.Drawing.Font theFont = ((MyFont)font).theFont;      //make the System API call     theGraphics.DrawString(word.text, theFont, Brushes.Block, point);   } } 

… however the implementation would need an downcast as illustrated above.

My question is, is there a way to do this without needing a downcast in the implementation? By ‘this’, I mean ‘defining UDTs like Word and Canvass which don’t depend on specific concrete System types’?

An alternative would be abstract UDTs …

class Word {   //System.Drawing.Font font; //declared in a subclass of Word   string text; }  class Canvass {   //System.Drawing.Graphics graphics; //declared in a subclass of Canvass   //concrete draw method is defined in a subclass of Canvass   internal abstract void draw(Word word, Point point);  } 

… but this too would need a downcast in the implementation of the subclass.

I also thought of using the double dispatch idiom, but it depends on naming the various subclasses in the APIs.

Or, if not with interfaces or subclasses, is there some way using delegates?


–Edit:–

There have been two possible answers.

One answer is to use generics, precisely as suggested by ‘Sir Lantis’ answer below, and as suggested by the blog post to which John Skeet linked. I suspect this would work fine in most scenarios. The down-side from my point of view is that it means introducing TFont as a template parameter: it isn’t only a class like Word (which contains a Font instance) which needs to become a generic class (like WordT<TFont>) … it’s also that any class which contains a WordT<TFont> (e.g. Paragraph) now also needs to become generic with a TFont parameter (e.g. ParagraphT<TFont>). Eventually, almost every class in the assembly has become a generic class. This does preserve type-safety and avoid the need to downcast … but it’s kind of ugly, and disturbs the illusion of encapsulation (the illusion that ‘Font’ is an opaque implementation detail).

Another answer is to use a map or dictionary in the user class. Instead of Font in the reusable library, and instead of an abstract interface, define a ‘handle’ class like:

public struct FontHandle {   public readonly int handleValue;   FontHandle(int handleValue)   {     this.handleValue = handleValue;   } } 

Then, instead of downcasting from FontHandle, keep a Dictionary<int, Font> instance which maps FontHandle values to Font instances.

  • 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. 2026-05-11T02:45:04+00:00Added an answer on May 11, 2026 at 2:45 am

    First, I wonder if the entire scenario isn’t a little artificial; are you really going to need this level of abstraction? Perhaps subscribe to YAGNI?

    Why does your MyGraphics only work with a MyFont? Can it work with an IFont? That would be a better use of interfaces, and would avoid this entire issue…

    One option might be a bit of a re-design, so that the IFont just describes the metadata for the font (size, font-face, etc), and you have things on the concrete MyGraphics like:

    [public|internal] MyFont GetFont(IFont font) {...} // or just Font 

    and it becomes the job of the graphics to do the translation – so then used something like:

    public void drawString(string text, IMyFont font, Point point) {     using(System.Drawing.Font theFont = GetFont(font))     {         theGraphics.DrawString(word.text, theFont, Brushes.Block, point);     }     // etc } 

    Of course, Point might need translation too ;-p

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • 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
  • added an answer Split off a method stop() from stopAndRemove(). Then write the… May 11, 2026 at 9:58 am
  • added an answer As Noah states, setuptools isn't an automake package so doesn't… May 11, 2026 at 9:58 am
  • added an answer I think that Controls.ContainsKey(...) is always returning false, because you… May 11, 2026 at 9:58 am

Related Questions

I have some logic, which defines and uses some user-defined types, like these: class
I have some code in an IAuthorizationFilter which redirects the user to a login
I have a LinkButton that has to postback to perform some logic. Once it
I have to login in to various elements to retrieve some data, problem is
I have some UI in VB 2005 that looks great in XP Style, but
I have some ASP.NET web services which all share a common helper class they
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some classes layed out like this class A { public virtual void
I have some C# / asp.net code I inherited which has a textbox which
I have some code like this in a winforms app I was writing to

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.