B4J Library jBasicLib library

wl

Well-Known Member
Licensed User
Longtime User
When I try to build the demo application I get an error on "MsgBoxes" (not found).

I'm running B4J 1.5

Thanks
 

wl

Well-Known Member
Licensed User
Longtime User
I knew I was missing a library but could not find it. Now it's working. Thanks !
 

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I'm trying the demo app and inserted some testing scripts but run into some strange errors

B4X:
Dim b(2)
b(0) = "hello"
b(1) = "world"
Foo (b)

Sub Foo(c)
    MsgBox(c(0))
    MsgBox(c(1))
End sub

-->local variable can not be an array when the second MsgBox is to be shown
How can you pass arrays to subs ?


B4X:
For i = 0 to 1
    MsgBox(i)
Next

-> No program line to be executed after NEXT

Thanks !
 
Last edited:

wl

Well-Known Member
Licensed User
Longtime User
OK, I found something out:

This works

B4X:
Dim b(2)
b(0) = "Hello"
b(1) = "World"
Foo ("b")

sub Foo(a)
for i = 0 to GetTotalLen(a) - 1
MsgBox (GetItem (a, i))
next
end sub

But still this does not work (no possibility to have arrays as local variables ?):

B4X:
Foo2()

Sub Foo2()
 Dim b(2)
 b(0) = "Hello"
 b(1) = "World"
 Foo ("b")
End Sub

sub Foo(a)
 for i = 0 to GetTotalLen(a) - 1
 MsgBox (GetItem (a, i))
 next
end sub
 

agraham

Expert
Licensed User
Longtime User
How can you pass arrays to subs ?
You can't, and don't need to. Arrays must be declared as Globals in the outer scope but can be re-Dimmed in Subs if necessary. Have you read the "Variables" section in the B4Script.chm help file?

The tokeniser needs to fill in a target line for the For statement to jump to on exit, a blank line is sufficient. I guess your Next was the last statement in the outer scope - hence the error message.
 

wl

Well-Known Member
Licensed User
Longtime User
Thanks agraham,

But I tend to disagree that "you don't need to pass arrays to subs"... Having all kinds of global variables just is something you would want to avoid.

Nevertheless: great work !
 

agraham

Expert
Licensed User
Longtime User
But I tend to disagree that "you don't need to pass arrays to subs"
I didn't intend that as a blanket statement of philosophy but meant that in this particular case you can always use a "scratch" global array as a local array . This script language almost exactly mirrors, very intentionally, that of the now obsolete Basic4ppc as it was originally written as a scripting library for Basic4ppc applications.
 
  • Like
Reactions: wl

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Still experimenting ...

I noticed I run into some strange errormessages when I have a space between the name of a sub and the opening parenthesis:

B4X:
Foo("Hello world")

rem NOTICE THE SPACE BELOW between Foo and (
Sub Foo (b)
  MsgBox (b)
End sub
--> NumberFormatException

When I remove the space it's all OK. Is this intended ?

Thanks
 

agraham

Expert
Licensed User
Longtime User
It's not intended but in all the years since I originally wrote this I never noticed it as I never put a space before the parenthesis. :(.

It looks like the the space in the Sub definition is causing the first pass of the tokeniser to fail to recognise that Foo is a Sub so at runtime it thinks the call to Foo is an array access, hence the NumberFormatException.

Let's call it a feature! :)
 

wl

Well-Known Member
Licensed User
Longtime User
Would be nice to fix it and give me a free license for noticing the '"feature" :)

The code example in the helpfile also contains a space :)

B4X:
Sub ShowMean
            Msgbox("The mean of 20 and 30 is " & crlf &  Mean(20,30))
End Sub


Sub Mean (a, b)
            Return (a+b)/2
End Sub
 

wl

Well-Known Member
Licensed User
Longtime User
Hi,

One other question: is there a possibility in a host method (called by 'callsub') to access a global array (based on it's name or something) ?

Thanks !!
 

wl

Well-Known Member
Licensed User
Longtime User
Ok, I found it:

you can use:
Basic.GetArrayLength(<varname>)
Basic.GetArrayItem(<varname>, index)
...
 
Top