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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:24:32+00:00 2026-05-31T16:24:32+00:00

I’m having some trouble conceptualizing what I’m supposed to do to facilitate communication between

  • 0

I’m having some trouble conceptualizing what I’m supposed to do to facilitate communication between my class library and programs that use it — in this case, a Windows Forms app:

// Class in library
class Foo()
{
    public Foo(){}

    public void DoWork()
    {
        log("Working...");
    }

    private void log( string s )
    {
        Console.Writeline(s);
    }

}

// Forms App
class Form1()
{
    public Form1()
    {
        Foo MyFoo = new Foo();
        MyFoo.DoWork();
    }
}

Since there is nothing listening to the console in a winforms app, calls to log() show nothing. Is there a way to dynamically overwrite the Foo.Log method, or possibly assign a method with that signature to the Foo object that is more suitable for a forms app at runtime?

Thank you!

  • 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-31T16:24:33+00:00Added an answer on May 31, 2026 at 4:24 pm

    Declare a logger interface and inject a concrete logger into the library class

    public interface ILogger
    {
        void Log(string message);
    }
    
    public class Foo
    {
        private ILogger _logger;
    
        public Foo (ILogger logger)
        {
            _logger = logger;
        }
    
        public void DoWork ()
        {
            _logger.Log("Working...");
        }
    }
    

    Use it like this

    var foo = new Foo(new ConsoleLogger());
    var foo = new Foo(new FileLogger());
    var foo = new Foo(new MsgBoxLogger());
    

    Where all these loggers implement the interface.

    public class ConsoleLogger : ILogger
    {
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
    }
    

    UPDATE

    As Mike Panter has pointed out already, you can use a delegate

    public class Foo
    {
        private Action<string> _writeLog;
    
        public Foo (Action<string> writeLog)
        {
            _writeLog = writeLog;
        }
    
        public void DoWork ()
        {
            _writeLog("Working...");
        }
    }
    

    you can call it like this

    var foo = new Foo(s => Console.WriteLine(s));
    

    Advantages of the delegate injection

    • Very simple to declare
    • Very simple to use
    • You can pass additional parameters through variable capturing.

            See Jon Skeets article The Beauty of Closures for variable capturing.

    Advantages of the interface injection

    • You can easily pass parameters to the logger through its constructor, like a file name for the FileLogger. Since the constructor is not part of the interface, different loggers can have different constructor parameters.
    • You can easily test it with unit tests, what you cannot do with anonymous lambda expressions, since there is no way to access them from the unit test.
    • You can use a mock logger in unit tests of your library classes.
    • You can create a new specialized logger by deriving it from an existing one.
    • You can declare private helper methods within a logger.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.