How can I identify an Array

enonod

Well-Known Member
Licensed User
Longtime User
Short of wasting an entry in an Array as an identifier, how can I know which it is?

aOne and aTwo are the arrays and I pass them randomly to a Sub. In the Sub I need to know which one was passed and...

Sub X (aFred() As Int)
If (aFred=aOne) Then

does not seem to work.
 

enonod

Well-Known Member
Licensed User
Longtime User
Sorry Erel, did you finish the sentence? I am still lost.
I was converting a Java program I had written except that it was
If aFred==aOne and it was working.
I actually don't understand why it doesn't work here nor do I comprehend your solution for an alternative.
Could you please expand.

The error I got was...
src1\main.java:421: inconvertible types
found : int[][]
required: double
if ((_awhich==(double)(_afred))) {

but with a little arrow under the ( before _agrid
I do not understand what it is telling me. Both arrays are the same

The code...
B4X:
Sub Globals
   Dim aFred(x,y), aTest(x,y) As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   x=45: y=45
   aFred(20,20)=1 : aFred(21,20)=1 : aFred(21,20)=1 
'The above line pauses LastExeption java.lang.ArrayIndex out of bounds
   ...
End Sub

Sub Draw(aWhich(,) As Int)
Dim b As Int
   If (aWhich=aFred) Then b=b+1  'I removed this to run and got the pause error above
   ...
End Sub
Thank you
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Try this:

B4X:
Sub Globals
   Dim x as Int : x = 45
   Dim y as Int : y = 45
   Dim aFred(x,y) as Int
   Dim aTest(x,y) As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   aFred.Initialize
   aFred(20,20)=1 
   aFred(21,20)=1 
   aFred(21,20)=1 
End Sub

Sub Draw(aWhich(,) As Int)
    Dim b As Int
    b = 0
   If (aWhich=aFred) Then b=b+1  
End Sub
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for the assistance.
I was not aware of Array initialize and cannot find it anywhere in the docs. [EDIT] and in fact causes an error.
I would be grateful if you could point out my mistakes.

I thought that the dimensions though not declared as values immediately, seem to have been accepted because the mouse Hint showed the correct values (45) for the dimensions when the program paused. They were declared before the attempt to fill the array, so I am a bit in the dark.
A little more guidance would be appreciated, the program does not now raise the Array Index error using your declaration.

I still get the compile error in the Draw Sub
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
To be honest I just took a stab in the dark and cleaned up your code a little to make it more understandable.

Usually things declared in Activity_Create and Process_Globals act as if they are declared together but in my opinion it is better to declare them all in one place (Globals for declarations, Activity_Create for code).
So I am sure the Arrays were being declared the right sizes, but I was not sure of the types.

The error you are getting suggests one of the operands in the comparison is a double and the other is an integer.
Maybe Draw is a reserved sub? Try renaming it.

But on the other hand, I think the better solution is to used two arguments to your sub, and use the second argument to tell it which array you are passing.

B4X:
Sub DrawItem(theArray(,) as Int, theArrayName as String)
   If (theArrayName = "aFred") then b = b + 1
   ....
End Sub

and call it with:
B4X:
Sub Activity_Create
   aFred(20,20) = 1
   
   DrawItem(aFred, "aFred")

End Sub
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
There obviously was something wrong with the declaration that rearranging has cleared somehow.
I have a suspicion that you may be right with 'Draw' being reserved but I will take on board the suggestion of passing a second variable and it seems that it was that idea that Erel was trying to convey to me but I missed totally.

All I can say is thank goodness for the patience and generosity of forum members.
Thank you for your help
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I will take on board the suggestion of passing a second variable and it seems that it was that idea that Erel was trying to convey to me but I missed totally.
Erel is a man of few words, but every word is weighed in gold.
If I dont understand what he says I google every word of his sentence to figure it out ;)
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
I could not agree more.
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
B4X:
Sub Globals
   Dim aFred(x,y), aTest(x,y) As Int
End Sub

I believe the basic problem is the declaration of the aFred and aTest variables. Dim-ing requires you insert the size of the array, not the variable to be used to access it. Should be:

B4X:
Sub Globals
   Dim aFred(45,45), aTest(45,45) As Int
End Sub

Of course, you would use whatever size value YOU need!
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for your input. I am afraid this still has a way to run yet.
I tried using digits and it does not cure the problem.
In fact, with the arrays declared with variables, I pause the program in debug and when I hover the mouse over the array name, all the content of the array is shown in the mouse hint, proving that the array has been created correctly.
Naturally I commented the receiving Sub out because this is a compile error not a runtime error.

Whilst I accept there is an easy alternative in passing another variable
I really need to find the reason for this problem to do with 'double' when it is declared as Int.
For clarity I have isolated the code, simplified and rewritten as a test...
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim aFred(4,4), aTom(4,4) As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   aFred(0,0)=1 : aTom(0,0)=2
   Test(aFred)
End Sub

Sub Test(aWhich(,) As Int)
   Dim b As Int
   b=0
   If (aWhich=aFred) Then b=1
End Sub
The error suggests that aWhich should be Double not Int. The little arrow should be under the 4th open parenthesis.
Compiling code. 0.02
Generating R file. 0.00
Compiling generated Java code. Error
B4A line: 30
If (aWhich=aFred) Then b=1
javac 1.6.0_26
src\Tests\Test1\main.java:335: inconvertible types
found : int[][]
required: double
if ((_awhich==(double)(_afred))) {
^
1 error
In Java this works OK
B4X:
void Test(int[][] which){
  b=(which==grid)?0:b;
Surely somebody has used this same technique without problem, if not, then a bug?
 
Last edited:
Upvote 0
Top