You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

theuserbl

Intermediate

  • "theuserbl" started this thread

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

1

Saturday, September 11th 2010, 8:31pm

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:

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.
theuserbl has attached the following file:
  • DimInLoopBug.zip (4.43 kB - 184 times downloaded - latest: Sep 15th 2017, 1:01pm)

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Saturday, September 11th 2010, 9:20pm

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

theuserbl

Intermediate

  • "theuserbl" started this thread

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

3

Saturday, September 11th 2010, 9:36pm

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

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.

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

4

Sunday, September 12th 2010, 10:10pm

Look at this (artificial) sample:

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

theuserbl

Intermediate

  • "theuserbl" started this thread

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

5

Tuesday, September 21st 2010, 3:00pm

Thanks A 1880. That works. :)

Rate this thread
WoltLab Burning Board