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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:04:33+00:00 2026-06-15T10:04:33+00:00

I have looked around some now to find a solution to this problem. I

  • 0

I have looked around some now to find a solution to this problem. I found several ways that could solve it but to be honest I didn’t realize which of the ways that would be considered the “right” C# or OOP way of solving it. My goal is not only to solve the problems but also to develop a good set of code standards and I’m fairly sure there’s a standard way to handle this problem.

Let’s say I have 2 types of printer hardwares with their respective classes and ways of communicating: PrinterType1, PrinterType2.

I would also like to be able to later on add another type if neccessary. One step up in abstraction those have much in common. It should be possible to send a string to each one of them as an example. They both have variables in common and variables unique to each class. (One for instance communicates via COM-port and has such an object, while the other one communicates via TCP and has such an object).

I would however like to just implement a List of all those printers and be able to go through the list and perform things as “Send(string message)” on all Printers regardless of type. I would also like to access variables like “PrinterList[0].Name” that are the same for both objects, however I would also at some places like to access data that is specific to the object itself (For instance in the settings window of the application where the COM-port name is set for one object and the IP/port number for another).

So, in short something like:

In common:

  • Name
  • Send()

Specific to PrinterType1:

  • Port

Specific to PrinterType2:

  • IP

And I wish to, for instance, do Send() on all objects regardless of type and the number of objects present.

I’ve read about polymorphism, Generics, interfaces and such, but I would like to know how this, in my eyes basic, problem typically would be dealt with in C# (and/or OOP in general).

I actually did try to make a base class, but it didn’t quite seem right to me. For instance I have no use of a “string Send(string Message)” function in the base class itself. So why would I define one there that needs to be overridden in the derived classes when I would never use the function in the base class ever in the first place?

I’m really thankful for any answers. People around here seem very knowledgeable and this place has provided me with many solutions earlier. Now I finally have an account to answer and vote with too.

EDIT:

To additionally explain, I would also like to be able to access the objects of the actual printertype. For instance the Port variable in PrinterType1 which is a SerialPort object. I would like to access it like:
PrinterList[0].Port.Open()

and have access to the full range of functionality of the underlaying port. At the same time I would like to call generic functions that work in the same way for the different objects (but with different implementations):

foreach (printer in Printers)
    printer.Send(message)
  • 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-06-15T10:04:34+00:00Added an answer on June 15, 2026 at 10:04 am

    a Pseudo-example using interfaces.

    public interface IPrinter
    {
       void Send();
       string Name { get; }
    }
    
    public class PrinterType1 : IPrinter
    {
      public void Send() { /* send logic here */ }
      public string Name { get { return "PrinterType1"; } }
    }
    
    public class PrinterType2 : IPrinter
    {
      public void Send() { /* send logic here */ }
      public string Name { get { return "Printertype2"; } }
    
      public string IP { get { return "10.1.1.1"; } }
    }
    
    
    // ...
    // then to use it
    var printers = new List<IPrinter>();
    
    printers.Add(new PrinterType1());
    printers.Add(new PrinterType2());
    
    foreach(var p in printers)
    {
      p.Send();
    
      var p2 = p as PrinterType2;
    
      if(p2 != null) // it's a PrinterType2... cast succeeded
        Console.WriteLine(p2.IP);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked around but have yet to find a great solution the the following
I looked around but haven't found an answer to this. I have a CentOS
I have looked around the internet for 3 hours now looking for a solution
I have looked around SO and the web for quite some time now and
I've got zero experience with Python. I have looked around some tutorial materials, but
I have looked around for quite some time trying to get openCV installed. I'm
I have looked around on Google and StackOverflow for the answer to this question,
I have looked around here and the Internet in general but it seems this
Sorry if this is obvious but have looked around and can't get this working:
I have looked all around and only found solutions for python 2.6 and earlier,

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.