Dim-ing variables

colin9876

Active Member
Licensed User
I wanted to draw a moving polygon
to store the 5 coordinates I did
dim x(5)
dim y(5)
Q1) rather than saying x(0)=0 x(1)=100 x(2)=120 x(3)=140 etc....
is there a quick way assigning the array such as> x()=[0,100,120,140,80]

Q2) The polygon command errored - what is wrong with
form1.polygon(x(),0,y(),0,5,cBlue)
which should be the right format to say x() starting at 0, y() starting at 0,count 5, in colour Blue)
But it didnt work, please help?
 

colin9876

Active Member
Licensed User
ok with a triangle
dim x(3)
dim y(3)
x(0)=0 y(0)=0 x(1)=0 y(1)=100 x(2)=200 y(2)=100
form1.polygon(x(),0,y(),0,3,cRed)
Error description: ArgumentNullException
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Works fine here (don't forget to create a form named Form1).
B4X:
Sub Globals
    'Declare the global variables here.
    Dim x(3),y(3)
End Sub

Sub App_Start
    x(0)=0 :y(0)=0 :x(1)=0: y(1)=100: x(2)=200: y(2)=100
    form1.polygon(x(),0,y(),0,3,cRed)
    Form1.Show
End Sub

I prefer to use a structure:
B4X:
Sub Globals
    'Declare the global variables here.
    Dim Type(x,y) points(3)
End Sub

Sub App_Start
    points(0).x = 0
    points(0).y = 0
    points(1).x = 0
    points(1).y = 100
    points(2).x = 200
    points(2).y = 100
    form1.polygon(points(),0,3,cBlue)
    Form1.Show
End Sub
 

colin9876

Active Member
Licensed User
Silly me,
I had set the values of x(0) etc in the Sub Globals and not the App_start, I thought you could define the values of variables in either! Ive seen it done before like in clock prog
Sub Global
mytime=0
End Sub

Is it different for array values? Anyway it works now - Thanks
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I thought you could define the values of variables in either!
You should be able to! This works for me.

B4X:
Sub Globals
   'Declare the global variables here.
   Dim array(3)
   array(0) = 1
   array(1) = 2
   array(2) = array(0) + array(1)
End Sub

Sub App_Start
   Msgbox(array(2)) ' displays 3
End Sub
 

colin9876

Active Member
Licensed User
Sorry guys, mystery solved ... but how weird is this

At first I had these (faulty) lines in the Sub global -
x(0)=0 y(0)=0 .....
x(1)=0 y(1)=100

But the error reported at the polygon statement


When I moved them to the App sub
The error reported at the first
x(0)=0 y(0)=0
now I realise there should have been a : between them when setting two variables on the same line.

But for some reason the error message came at a different point when my coding error was in the different Subs!

i.e. the x(0)=0 y(0)=0 didnt error immediately when its in the Global sub but it did in the App sub

Totally my mistake, but thrown by the differnt error reporting???
 
Last edited:
Top