Hi from Wales, UK. My first time on the forum so bear with me. I’ve been learning Xojo over the last 12 months or so in readiness to completely rewrite our in-house EPOS system. I’ve solved most of the basic problems involved but would like some advice on one in particular.
I have one or more USB devices connected and have been manually adding the required qty of Serial Connection controls to the main window of the project. Current code all working fine but wondering if it’s worth using an array of controls (based on SerialConnection) which would be dynamically added at runtime based on the number of devices connected. The number would vary depending on the site concerned at the time although the absolute maximum would only be 3.
I’ve had a quick look at implementing this but not got very far- is it worth the extra effort and am I going to gain any ‘efficiency’ in the final project? Worse case is I have 3 SerialConnection controls but only use 1.
Hope that makes sense. Any advice appreciated, thanks
If the alternative is SerialConnection1, SerialConnection2, etc, then definitely yes! Then there is no ‘worst case’.
It depends on how much you’ve picked up on the Object Oriented and Event Driven design concepts in the last 12 months.
Xojo has such a wide range of skill levels it’s hard to make a “should I do this” recommendation without knowing where yours is. There are people here who have been using Xojo for 20 years that still don’t grasp these concepts.
If you are familiar with them, making use of these concepts will help you write more straightforward and maintainable code. For a max connection count of 3, I don’t think you’ll see a performance gain. But I do think the code will be easier to work with, especially when you need to revisit the project in 6 months to fix something.
Thanks, both replies taken on board!
I’m very much working on the basis that it should be to ‘best practice’ - either in terms of performance or coding in general (or both of course). I will invest some time on this in the following week or so
I hope it’s ok to post any queries regarding this on the same post if required.
The answer to your question depends largely on how you are interacting with them. If you already a a 3 element array that works, then going to a dynamic array is trivial. If you have code that addresses them by name, and not by myarray(n), then it will take some work.
My initial test code addresses them by name so I’m expecting it to take some time. My initial attempts today have ended in a grinding halt as apparently AddControl can only be used to add desktop control at runtime. So is it even possible to add a SerialConnection control in code?
You wouldn’t use AddControl. SerialConnections are not a UI element. The IDE lets you add them to a Window for convenience, but the way you will use them in code is as an array.
Create an array property to store the connections, you can keep it on the Window if it’s specific to the Window, or use a Module to make them Globally accessible if that is necessary. The parens after the property name indicate it’s an array.
Anything that shows up in the “bottom bar” is not a UI element and gets used this way.
If your code is the same for all connections, then an array is definitely recommended.
Even better would probably be an Array of a SerialConnection subclass that has all the necessary properties and methods…
If you later revise the code, add to it, etc., you only need to do this in the one SubClass.
So instead of maintaining connection_1, connection_2, connection_3, you create 1 connection() and add and remove it dynamically during runtime.
In theory, you then no longer have to worry about whether the app needs 0, 1 or 10 connections…
Some answers are so blindingly obvious when spelled out by somebody else
Thanks for all the pointers. Actually turned out far less work than I imagined so all good!