<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Oop best practice</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dd9b88d755/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dd9b88d755/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/dd9b88d755/</id><updated>2025-10-16T11:37:23.491000Z</updated><subtitle>Recent posts to Oop best practice</subtitle><entry><title>Oop best practice</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dd9b88d755/?limit=25#8c3b" rel="alternate"/><published>2025-10-16T11:37:23.491000Z</published><updated>2025-10-16T11:37:23.491000Z</updated><author><name>xcqt</name><uri>https://forge.codesys.com/u/xcqt/</uri></author><id>https://forge.codesys.comee0cc00743064ae9207db7ae55e81c027014ed05</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi all,&lt;/p&gt;
&lt;p&gt;I’m currently trying to improve my OOP structure in CODESYS and I’m looking for some input on how others approach this.&lt;/p&gt;
&lt;p&gt;I understand the basics like inheritance, interfaces, abstract FBs, methods, and properties, but I still struggle a bit with the overall architecture and what’s considered clean or scalable in bigger projects.&lt;/p&gt;
&lt;p&gt;As an example, I’m working on two different energy meter function blocks:&lt;/p&gt;
&lt;p&gt;FB_EnergyMeter_MQTT reads data from MQTT (strings)&lt;br/&gt;
FB_EnergyMeter_Modbus reads data from Modbus (words)&lt;/p&gt;
&lt;p&gt;Both have their own Update() method and implement the same interface (something like IF_EnergyMeter). Later on, I’ll probably add more meter types, but they should all behave the same from the controller’s point of view.&lt;/p&gt;
&lt;p&gt;Now, there’s a FB_GridControl block that needs power data from these meters. I see two options here:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Define the meter blocks inside FB_GridControl and call them directly (for example fbModbusMeter.UpdateModbus()).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Keep the meter blocks outside and pass them into FB_GridControl as interface references, so the control block doesn’t know which specific type of meter it’s dealing with.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Option 2 feels cleaner and more flexible to me, but I’m not entirely sure how to handle the data flow in that case. Should I pass the meter instance through an interface reference (REFERENCE TO IF_EnergyMeter)? Or is there a better way to link the external FBs to the control block?&lt;/p&gt;
&lt;p&gt;I’d like to hear how you structure this kind of setup or see an example from someone who has done something similar.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;br/&gt;
Thomas&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>