Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Allow Simple Variable or Array as parameter?
Author | Message | ||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1109 |
Numerous MMBasic commands allow either simple variables or arrays to be passed as parameters. Is there any way to accomplish this in our own programs? Could you pass the location of a variable, then look there to determine what kind of variable it is? (Simple or array, number of dimensions, int/float/string etc.) For that to be truly useful, we'd need a way to make a local variable point to somewhere else, and in doing so, possibly change its type. Of, course, when the routine ends, garbage collection gets interesting - don't dump the wrong thing! As an aside, if you attempt BOUND(simplevar()) you get an error as simplevar is not an array. BOUND(simplevar) works as expected but then fails if simplevar is actually an array. I suppose you could ONERR SKIP to trap it, but that is kinda evil. Edited 2024-01-10 13:47 by vegipete Visit Vegipete's *Mite Library for cool programs. |
||||
toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 339 |
Have you tried BOUND(simplevar(),1) for the first dimension? Here's an example from the CMM2 manual. Maybe it works for the 'mite as well? DIM myarray(44,45) BOUND(myarray(),2) will return 45 |
||||
EDNEDN Senior Member Joined: 18/02/2023 Location: United StatesPosts: 118 |
It seems to me the BOUND() function should be tolerant of being passed either an array name or a variable name. And if that were true, asking for the size of the first dimension would tell you what you were dealing with. Specifically, It would make sense to me to allow either BOUND(xyz,1) or BOUND(xyz(),1) if xyz is an array. And of course, both of those should return the same value. That would allow the code to adapt and easily handle the situation. Edited 2024-01-11 13:14 by EDNEDN |
||||
William Leue Guru Joined: 03/07/2020 Location: United StatesPosts: 393 |
It's a bit clumsy, but you could always do: ON ERROR SKIP 1 N = BOUND(PASSED_VAR) IF MM.ERRNO <> 0 THEN ' You know it's not an array ELSE ' You know it's an array END IF -Bill |
||||
Print this page |