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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:09:41+00:00 2026-05-16T17:09:41+00:00

I’ve been struggling to get my mind around classes. Even after reading several websites

  • 0

I’ve been struggling to get my mind around classes. Even after reading several websites and books, I still don’t feel like I really ‘get it’.

The application that I’m working on has a sub that will control a few different pieces of test equipment over a USB to GPIB interface.

So, I have:

Laptop
   USB - GPIB Interface
        Power Supply (Device Address 5)
        HP34970 Datalogger (Device Address 6)

In my sub I would like to Open the GPIB device, send a few SCPI commands to turn on the power supply, send a few more to the HP34970 to switch relays or measure voltage.

Seems simple enough and I can easily make everything work in the same module. However, I think I it would be much better to have a separate class for the GPIB Interface, Power Supply, and HP34970. If this was the case, I could very easily reuse the code in other projects.

Here is where I just can’t get a mental model- If I create an instance of the GPIB class and have a method to ‘open’ a channel to the GPIB bus, how can I allow methods in other classes (like power supply) to use the open channel created in the GPIB class? For example, a method in the power supply class to set voltage.

If someone anyone could take a few minutes to post pseudo code and a little explaination to illustrate how I could / should organize this, I would greatly appreciate the help!

Thanks

  • 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-16T17:09:41+00:00Added an answer on May 16, 2026 at 5:09 pm

    Think of classes as devices themselves. The main benefit here would be to be able to reuse code written once – for example, all of your devices have addresses, they can connect, test connection, disconnect – why not have that in one abstract “Device” class, and make all devices inherit from it?

    This could be one of the usages – written in C# (sorry if I misunderstood the actual usage 🙂 here I’m assuming powersupply and dataLogger are connected to the interface, and interface to the laptop).

    public class DeviceTesting
    {
       private string powerSupplyAddress = "DA5";
       private string dataLoggerAddress = "DA6";
    
       public static void main()
       {
    
          //bring interface into life
          DeviceInterface interface = GPIBInterface(); //example of inheritance - GPIB could be just one of the many device interfaces
    
          //the constructor in PowerSupply takes on the responsibility of initializing itself
          PowerSupply ps = new PowerSupply(powerSupplyAddress, interface); //plugged in with interface - has a reference to it
          //you could have multiple types of data loggers - all could inherit from DataLogger, and here so does HP34970
          //This means you can reuse the common code for data loggers, and write only the specifics for each specific device of that kind
          DataLogger myLogger = new HP34970(dataLoggerAddress, interface);
    
    
          //now, do whatever you do with the real devices
          ps.SetVoltage(220); //send voltage command directly
          interface.SendLogMessage("Interface is operational");
          interface.ExecuteTest("test1"); //send voltage command through interface
          //etc... you get the idea...
       }
    }
    

    Now, since the interface has the knowledge of the devices it’s interfacing with, it has to have them in it’s constructor (in object oriented design, also known as Dependency injection, more specifically here it’s Constructor Injection):

    public GPIBInterface : DeviceInterface //here we are reusing code that's same for all interfaces
    {
        //whoever attaches to this interface is responsible for registering itself with it
        public PowerSupply Power{get;set;}
        public DataLogger Logger{get;set;}
        //notice here that it can work with any class that inherits from DataLogger
        public GPIBInterface()
        {
    
        }
    
        private override void TestInitialization() //as an example, we write our own testing by overriding some method in base class
        {
            base.TestInitialization(); //make sure that the base class checks it's ok - e.g. if it's own display is working or similar...
    
            if (Power.TestConnection() == false)
                throw new DeviceNotWorkingException(ps);
            if (Logger.TestConnection() == false)
                throw new DeviceNotWorkingException(_logger);
        }
    
        public void SendLogMessage(string message)
        {
            Logger.SendMessage(message);
        }
    
        public void ExecuteTest(string testName)
        {
            switch(testName)
            {
                case "test1":
                    Power.SetVoltage(280);
                    Logger.SendMessage("Voltage increased to 280V");
                    break;
            }
        }
    }
    

    Basically, if your devices interact with each other in ‘real life’, than it means they should have a reference to each other device they are connected to. For example, if you plugin PowerSupply directly to logger, than the PowerSupply class should have a reference to logger. But, if it’s connected to interface, and then interface to logger, the PowerSupply must have only reference to interface and not the logger (since it’s not connected to it).

    I hope you get the idea – it’s not called ‘object oriented’ for nothing – think of classes as real objects – if an object can do thing1, than you should have method ‘thing1()’ in you class; if the object has connection to object441, than the class should have a reference to that class. Just follow this principle and see where it takes you…

    When you master that, your program would really benefit from some Inversion of Control (IoC) pattern – that way you just define at the beginning what is a logger, what is powerSupply, who is performing as a display, and whenever someone needs some specific device an IoC container would provide it. This way you don’t have to rewrite code every time there’s a new device in town – just register the new type with IoC container and everything just works.

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

Sidebar

Related Questions

No related questions found

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.