2017년 1월 9일 월요일

How to combine three similar functions into a more generic single function?


Below picture shows a main function(updateLampPicker, left) and three very similar sub functions (updateReagenz1/2/3, right) and I am wondering if it is possibe to combined the three sub functions into a more generic sub function?

Pick function updateReagenz3 as an example.
I thought in order to set the global variable toggle_3 to false I would pass a string "3" to the more generic function and then iterate over all global variables and pick the one for which the name matches "toggle_r3", however I don't see how this would be possible.

Would appreciate any help in condensing the code as much as possible rather then replicating similar code snippets several times.

--
yes you can
so the only difference are these blocks

is ReagenzSel1-3 a label?
just only one procedure and pass the corresponding label component (this is the last item in the list of blocks) as input parameter
example:

then use the advanced label blocks to set the corresponding label component to its value

set ReagenzSel.Visible of component to true

see also

-- 
You can use TinyDB to generalize your arrays, in addition to using the all_components.
See attached for sample code.

This approach leaves out defaults for TinyDB access, so you have to initialize all entries or add a default parameter to the value procedure.



-- 
Thanks a lot Taifun and Abraham for the quick reply!
@Taifun: It is an ImageSprite.
I now understand how to make this more generic!

--
I ended up combining the global list definition from Taifun with the TinyDB replacement proposed by Abraham.
I can now replace my three functions (right) with the single more generic (left).

Of course I have to replace all the global variables (e.g. get global toggle_r1) by a corresponding TinyDB call get(tag="toggle_r1").

Thanks again for your quick help!

-- 

댓글 없음:

댓글 쓰기