BBC BASIC
« COMLIB failing parameter passing to FN_getvaluestr »

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 4:21pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: COMLIB failing parameter passing to FN_getvaluestr  (Read 757 times)
Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx COMLIB failing parameter passing to FN_getvaluestr
« Thread started on: Apr 25th, 2017, 5:43pm »

I'm reading data from an imported Excel file as strings. Some of the data need to be converted to numeric values. To do that I first replace the comma in the string A$ for a dot, then apply VAL A$. I'm sure there are more elegant ways to do this, but the following (simplified) code does the job nicely for me.
Code:
      INSTALL @lib$+"COMLIB"
      INSTALL @lib$+"STRINGLIB"
      BB_Exp_Sh$=@tmp$ + "BB_Exp.xls"
      MODE 26
      DIM B(100,6)
      PROC_cominit
      Xlapp% =FN_createobject("Excel.Application")
      Xlbook%=FN_getobject(Xlapp%,"Workbooks.Open("+CHR$34+BB_Exp_Sh$+CHR$34+")")
      FOR I%=1 TO 10
        FOR J%=1 TO 6
          B(I%,J%)=FNConvert(I%,J%)
        NEXT J%
      NEXT I%
      PROC_callmethod(Xlbook%,"Close(B FALSE,,)")
      PROC_callmethod(Xlapp%,"Quit")
      PROC_cleanup
      PROC_comexit
      END
      REM-----------------------------------------------------------
  111 DEFFNConvert(M%,N%)
      LOCAL A$,A%
  222 A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(I%,J%).value")
      A%=FN_findreplace(A$,",",".",0)
      =VALA$
      REM-----------------------------------------------------------
      DEF PROC_cleanup
      Xlapp% +=0 :IF Xlapp%  PROC_releaseobject(Xlapp%) :Xlapp% =0
      Xlbook%+=0 :IF Xlbook% PROC_releaseobject(Xlbook%):Xlbook%=0
 
Except ... when I change (I%,J%) to (M%,N%) in line 222 the program fails.
In the program listed above M% and N% are not used in the function, but if I modify line 222 to use the passed parameters instead of the global variables I% and J% it should work too.
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(M%,N%).value") 
Instead the message
Quote:
FN_getvaluestr failed on "ActiveSheet.cells(M%,N%).value" - EXCEPTION in module C:\Program Files (x86)\BBC BASIC for Windows\lib\COMLIB
appears. I can't figure out what am I doing wrong.

Eddy
User IP Logged

hellomike
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #1 on: Apr 25th, 2017, 7:00pm »

Never worked with COMLIB library myself, but hasn't it something to do with what is documented in the paragraph "Setting the correct data type" on the "COM automation" help page?
Maybe it sees 'N%' the same way as 'North%' as is described there.

But then, maybe it is not related.
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #2 on: Apr 25th, 2017, 7:39pm »

Quote:
Maybe it sees 'N%' the same way as 'North%'

It must be something else.
If you replace M% and N% by any other variable, say Q% and T%, the behaviour is the same.
Eddy
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #3 on: Apr 25th, 2017, 8:15pm »

on Apr 25th, 2017, 5:43pm, Edja wrote:
if I modify line 222 to use the passed parameters instead of the global variables I% and J% it should work too.

I would have expected the correct function call to be this:

Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells("+STR$(M%)+","+STR$(N%)+").value") 

Richard.
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #4 on: Apr 25th, 2017, 9:57pm »

Richard,

Yes, your line
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells("+STR$(M%)+","+STR$(N%)+").value") 
works perfectly.
But then, why is it that the line (with the global variables I% and J%)
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(I%,J%).value") 
also does work ? In the help pages I found this comment
Quote:
Note that in any case the passing of named variables is not compatible with compiling the program with the Abbreviate names crunch option, unless you use the REM!Keep compiler directive. It may therefore be better to convert the variables' values to strings using STR$
This hints towards the way you've coded the line, but since I was not compiling, this shouldn't apply in this case. Is this then a general rule or am I still missing the point ? My real program contains at least 50 instances of this type and it functions exemplary (with the exception discussed here)

I've modified my code as in your example.

Thank you!

Eddy


User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #5 on: Apr 25th, 2017, 10:22pm »

And just out of curiosity I've just replaced the capital letter variables M% and N% by m% and n%
Code:
      DEFFNConvert(m%,n%)
      LOCAL A$,A%
      A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(m%,n%).value")
      A%=FN_findreplace(A$,",",".",0)
      =VALA$ 
and this too functions perfectly, the advantage being that this is more readable and easier to debug. Still I will comply with the rules.

?
Eddy
User IP Logged

hellomike
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #6 on: Apr 26th, 2017, 08:29am »

Note also that in the FN_getvaluestr() definition in COMLIB, variables O%,L%,S%,R%,V% are used locally and more in functions that it needs, but not I% and J%. So I wouldn't be surprised that it will then substitute the wrong (local) values.

That would also explain why
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(I%,J%).value") 

and
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells("+STR$(M%)+","+STR$(N%)+").value") 

and
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(m%,n%).value") 
does correctly work.

Cheers
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #7 on: Apr 26th, 2017, 11:35am »

on Apr 25th, 2017, 9:57pm, Edja wrote:
But then, why is it that the line (with the global variables I% and J%) also does work ?

COMLIB tries to help you out by using EVAL to discover the value(s) of any variables passed (by name) in parameter strings, but obviously that can only work when the variable concerned still has that value at the point EVAL is used. This will not (generally) be the case for the static integer variables (A% to Z% inclusive), and some other short variable names, because they may well be used locally within COMLIB.

The reason I% and J% worked for you is a fluke: it must be the case, by chance, that neither of those variables is used within COMLIB as a LOCAL or PRIVATE variable, or as a formal parameter. There is no guarantee at all that your program would survive a change to COMLIB, because there is nothing 'contractual' about which variables it uses internally.

The other reason that passing variables by name will commonly fail is as a result of 'crunching', when the variable name(s) within your parameter string no longer correspond with the actual variable name(s) in the program.

So the general rule is that any variables passed within parameters in this way must be specified in a REM!Keep compiler directive. Consequently such variables must meet the naming rules for REM!Keep, i.e. they "must have at least three characters (excluding the type character, if any)". The static integer variables like I% and J% do not meet this condition.

Perhaps the description of the library should state this more explicitly, but (and this was several years ago) I probably assumed that it was 'obvious' that static integer variables should not be passed to COMLIB 'by name'.

Richard.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #8 on: Apr 26th, 2017, 11:45am »

on Apr 26th, 2017, 08:29am, hellomike wrote:
variables O%,L%,S%,R%,V% are used locally and more in functions that it needs

As I said in my reply to Edja, you should not pass any static integer variables (or indeed any variable that does not meet the conditions for REM!Keep) 'by name' in a COMLIB parameter string.

Personally I prefer not to rely on COMLIB evaluating named variables at all, but rather to substitute the actual numeric value within the parameter string, as my suggested modification did. This is what you always have to do with OSCLI commands and it makes debugging easier.

Richard.
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #9 on: Apr 26th, 2017, 1:28pm »

Quote:
The reason I% and J% worked for you is a fluke ....
After your first reply I've already modified my code in line with your example. But I didn't completely understand why this was necessary and why my line (with I% and J%) did work.

Now I do. Thank you for this detailed answer.
Eddy
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #10 on: May 14th, 2017, 4:40pm »

A few weeks ago Malcolm has uploaded COMLIBZ, an adapted version of COMLIB. While at the time I had already sorted out (with your help) my problems, I did modify my existing program.
With COMLIBZ lines like
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells(M%,N%).value") 
no longer need to be written with encapsulation, as in :
Code:
A$=FN_getvaluestr(Xlbook%,"ActiveSheet.cells("+STR$(M%)+","+STR$(N%)+").value")  

Also the variable prefixes B, G, ... are no longer required (at least when working with Excel, as in my case) or are to be preceded with a *, giving more freedom in the choice of variable names.
Adapting my program took me about 5 min and it now is definitely more readable and easier to debug. I admit that I didn't test all possible scenarios and I didn't compile anything.
Considering the quality of the since long time available and tested COMLIB, this adapted version COMLIBZ is perhaps not really a major leap forward but rather a convenient face lift of the existing library. Nice !

Eddy


User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #11 on: May 14th, 2017, 5:06pm »

on May 14th, 2017, 4:40pm, Edja wrote:
A few weeks ago Malcolm has uploaded COMLIBZ, an adapted version of COMLIB.

It's the first I've heard of it, and a search for 'COMLIBZ' over a period of 777 days returns only your post! So I'm thoroughly puzzled. As far as I'm aware the only way to evaluate variables passed in a string is using EVAL, with all the attendant issues that raises.

Richard.
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #12 on: May 14th, 2017, 6:33pm »

Richard,

here's the mail that Malcolm send out to announce the availibility of COMLIBZ. I just followed the link(s).

Eddy

Quote:
Hello,

This email message is a notification to let you know that
a file has been uploaded to the Files area of the bbcbasic
group.

File : /Libraries/COMLIBZ.bbc
Uploaded by : mgmarten <mgmarten@...>
Description : A version of COMLIB what works correctly when static variables are used as parameters. Data types now need a leading "*" so "* F var" would indicate a floating point variable. "*B" not really needed, TRUE and FALSE need no prefix now.

You can access this file at the URL:
https://groups.yahoo.com/neo/groups/bbcbasic/files/Libraries/COMLIBZ.bbc

To learn more about file sharing for your group, please visit:
https://help.yahoo.com/kb/index?page=content&y=PROD_GRPS&locale=en_US&id=SLN15398

Regards,

mgmarten <mgmarten@...>
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #13 on: May 15th, 2017, 06:31am »

on May 14th, 2017, 6:33pm, Edja wrote:
here's the mail that Malcolm send out to announce the availibility of COMLIBZ. I just followed the link(s).

That seems to refer to the original 'bbcbasic' Yahoo! group (the pre-existing group that I temporarily 'adopted'), which was subsequently replaced by the 'bb4w' Yahoo group, which was in turn replaced by the 'groups.io' group! When you say that it was uploaded a few weeks ago, do you actually mean many years ago?

There are only three groups/forums currently used to support BB4W and BBCSDL: the Wiggio group (which is due to close down soon), the Groups.io group and this Conforums group. All other earlier groups are either inaccessible, closed or defunct.

Richard.
User IP Logged

Edja
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: COMLIB failing parameter passing to FN_getvalu
« Reply #14 on: May 15th, 2017, 10:21am »

Quote:
When you say that it was uploaded a few weeks ago, do you actually mean many years ago?
The upload dates from 30 april 2017.
Quote:
There are only three groups/forums currently used to support BB4W and BBCSDL
OK, but the Yahoo site is still accessible and as you can see Malcolm has succeeded to upload a file and sent a mail (to all members ?). I still have access to the Yahoo Group and could navigate around to find the file and download it.
I've just made a check. Since 2006 there have been no downloads.
Except : just recently since 30 april 2017 Malcolm has downloaded 4 .
Quote:
https://groups.yahoo.com/neo/groups/bbcbasic/files/Libraries/COMLIBZ.bbc
https://groups.yahoo.com/neo/groups/bbcbasic/files/Tools/DlgEditCE13.bbc
https://groups.yahoo.com/neo/groups/bbcbasic/files/Tools/WINCONSTlookup2_08.bbc
https://groups.yahoo.com/neo/groups/bbcbasic/files/Event%20Programming/Lib/VElib.bbc
Just follow the links. Maybe if you're not a member anymore, you may have to register first, I'm not sure.
Or, if you prefer, I can send you the files directly. Just let me know.
Quote:
There are only three groups/forums currently used to support BB4W and BBCSDL: the Wiggio group (which is due to close down soon), the Groups.io group and this Conforums group
The general conclusion, I think, is that Malcolm just needs to be reminded to upload to one of the new groups (Groups.io or this Conforum)
It would be a waste to loose his, and possibly, though not likely, other people's contributions

Eddy
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls