Dim in loop compiler-bug
Possible that it is already known. There is a Dim array in a loop bug.
Look at first in the code for command2:
This works fine.
Now implementing the first three lines as a for loop:
This gives out an error. The compiler compiles it wrong, because the "interpreter" of the compiler, which "understands" the code, don't work right.
It don't analyse which numbes i in MyEntry are.
In this part the compiler compiles this code part exactle like in Command1_Click(). So it is clear, where the bug comes from.
Look at first in the code for command2:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 |
Public Sub Command2_Click() Dim MyEntry[1] As New Class1 Dim MyEntry[2] As New Class1 Dim MyEntry[3] As New Class1 MyEntry[2].Name = "Hello" Dim MEnt As New Class1 MEnt.Name = "Hello" MsgBox (MEnt.Name) MsgBox(MyEntry[2].BooleanValue) End Sub |
This works fine.
Now implementing the first three lines as a for loop:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 |
Public Sub Command1_Click() For i = 1 To 3 Dim MyEntry[i] As New Class1 Next i MyEntry[2].Name = "Hello" Dim MEnt As New Class1 MEnt.Name = "Hello" MsgBox (MEnt.Name) MsgBox(MyEntry[2].BooleanValue) End Sub |
This gives out an error. The compiler compiles it wrong, because the "interpreter" of the compiler, which "understands" the code, don't work right.
It don't analyse which numbes i in MyEntry are.
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 |
Public Sub Command3_Click() MyEntry[2].Name = "Hello" Dim MEnt As New Class1 MEnt.Name = "Hello" MsgBox (MEnt.Name) MsgBox(MyEntry[2].BooleanValue) End Sub |
In this part the compiler compiles this code part exactle like in Command1_Click(). So it is clear, where the bug comes from.
What are you trying to achieve in the first place?
Your sample redefines a variable several times. I that on purpose?
Usually you define a variable just once with DIM and possibly REDIM it to change the array dimensions.
Your way of redefinition not only confuses the compiler but also the reader and maintainer of your code.
Am I missing something?
Greetings
A1880
Your sample redefines a variable several times. I that on purpose?
Usually you define a variable just once with DIM and possibly REDIM it to change the array dimensions.
Your way of redefinition not only confuses the compiler but also the reader and maintainer of your code.
Am I missing something?
Greetings
A1880
Right.
What I first tried was to create a class with variables (Class1) and then creating dynamical additional MyEntry[] 's of it.
First I tried something like
But that don''t work.
So I tried different things and different versions.
Dim MyEntry[1], Dim MyEntry[1], Dim MyEntry[2], etc. are all different names, so they have all be seperated with "Dim" defined. And the first version I mentioned (in Command2_Click()) works fine.
Sadly if I use instead of a definitly number a variable, it don't work anymore.
What I first tried was to create a class with variables (Class1) and then creating dynamical additional MyEntry[] 's of it.
First I tried something like
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 |
Dim MyEntry(1000) as new Class1 Dim i as integer ' Entry numkber Public Sub Command1_Click() MyEntry(i) = new MyEntry() MyEntry(i).name = "some name" .. etc. End Sub |
But that don''t work.
So I tried different things and different versions.
Dim MyEntry[1], Dim MyEntry[1], Dim MyEntry[2], etc. are all different names, so they have all be seperated with "Dim" defined. And the first version I mentioned (in Command2_Click()) works fine.
Sadly if I use instead of a definitly number a variable, it don't work anymore.
Look at this (artificial) sample:
The sample shows how to define and fill an array of object references,
You have used the "as new Class1" syntax which is only allowed for simple variables.
So, the Jabaco bug is that it does not warn you in this case.
I might be wrong but "MyEntry[2]" does not look like a valid variable name to me.
Variable names have to start with an alphabetical character.
The other characters can be alphabetical or numerical, but I have never seen squared brackets
in a Jabaco source code.
It is an interesting feature you have discovered: Jabaco allows direct definition of Java arrays
using the DIM xxx[n] notation with squared brackets. In contrast "Dim b(7)" with round brackets is compiled into
something like:
I've tried the following Jabaco code:
It is compiled to the following Java code:
Basically, the sample defines one array with 3 int entries.
However, this is not valid Java. Tools like Eclipse won't accept it.
In Java, it is not allowed to have local variables with duplicate names.
A proper array definition would look as follows:
Greetings
A1880
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Option Explicit Public Sub Command1_Click() Dim arr(10) As PictureBox Dim i As Integer Dim s As String For i = 1 To 10 arr(i) = New PictureBox Next i For i = 1 To 10 arr(i).ToolTip = i Next i s = "" For i = 10 To 1 Step -1 s = s & " " & arr(i).ToolTip Next i MsgBox s End Sub |
The sample shows how to define and fill an array of object references,
You have used the "as new Class1" syntax which is only allowed for simple variables.
So, the Jabaco bug is that it does not warn you in this case.
I might be wrong but "MyEntry[2]" does not look like a valid variable name to me.
Variable names have to start with an alphabetical character.
The other characters can be alphabetical or numerical, but I have never seen squared brackets
in a Jabaco source code.
It is an interesting feature you have discovered: Jabaco allows direct definition of Java arrays
using the DIM xxx[n] notation with squared brackets. In contrast "Dim b(7)" with round brackets is compiled into
something like:
|
|
Jabaco Source |
1 2 |
VBA.VBArrayDouble b = new VBArray(); b.setBound(0, 7, false); |
I've tried the following Jabaco code:
|
|
Jabaco Source |
1 2 3 4 5 |
Dim a[1] As Integer Dim a[2] As Integer Dim a[3] As Integer a[1] = 0 |
It is compiled to the following Java code:
|
|
Jabaco Source |
1 2 3 4 5 |
int a[1] = 0; int a[2] = 0; int a[3] = 0; a[1] = 0; |
Basically, the sample defines one array with 3 int entries.
However, this is not valid Java. Tools like Eclipse won't accept it.
In Java, it is not allowed to have local variables with duplicate names.
A proper array definition would look as follows:
|
|
Jabaco Source |
1 2 3 |
int a[] = new int[1]; a[0] = 0; |
Greetings
A1880
Similar threads
-
General topics, questions and discussions »-
populate listbox from text file
(Mar 26th 2010, 7:26pm)
-
General topics, questions and discussions »-
VBA#VBVariant
(Oct 26th 2009, 11:27am)
-
General topics, questions and discussions »-
Do it like ...
(Oct 16th 2009, 6:54pm)
