Discussion:
ReDim driving me crazy again…
Johnny Rosenberg
2011-08-17 10:41:53 UTC
Permalink
I edited a lot of code yesterday, one of the rows I did NOT edit was
ReDim NDice(1 To 6) As Integer
Public NDice(1 To 6) As Integer
The NDice variable is already defined.”
Yes, I know it is, that's one of the points by using ReDim, isn't it?
Before doing some heavy editing there was no complaints on this line
at all, and as I said, this is NOT one of the many lines I changed.
So obviously some of my other edits caused this, but where to start
looking? What can I possible have done to cause this?
I just hate when these things happen, when there is an error message
telling me that I can't do something that I should be able to do. Like
if I got an error message for the line ”If a=b Then”, and the error
message says: ”BASIC syntax error: a and b are already compared”…
I have now tried a couple of more things. I have changed the global
definition from Public to Private to Global to Dim and back again to
Public. Same result no matter what.
I have even removed the global definition, and I have searched through
ALL the modules after the variable to make sure it's not declared
elsewhere. STILL the error message says that it's already defined! I
would like to know where, because the Find thing (Ctrl+f) can't find
another definition of NDice than the ReDim on which the error occurs!
I have searched for Dim NDice, Public NDice, Private NDice and Global
NDice, nothing found except ReDim NDice (once – the one on which the
error occurs).

Well, I'll continue searching. I have now made a copy of the whole
document for wild debugging, so I will try the most brutal things I
can think of now, like removing entire modules to see if I can at
least find in what module I need to have a closer look at…

Any ideas are very welcome! I'm kind of losing my hair here… :P


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Johnny Rosenberg
2011-08-17 09:45:12 UTC
Permalink
I edited a lot of code yesterday, one of the rows I did NOT edit was
this one, which occurs at the top of a subroutine:

ReDim NDice(1 To 6) As Integer

It is defined in another module as:
Public NDice(1 To 6) As Integer


The error message I get is (translated from Swedish):
”BASIC syntax error:
The NDice variable is already defined.”

Yes, I know it is, that's one of the points by using ReDim, isn't it?

Before doing some heavy editing there was no complaints on this line
at all, and as I said, this is NOT one of the many lines I changed.

So obviously some of my other edits caused this, but where to start
looking? What can I possible have done to cause this?

I just hate when these things happen, when there is an error message
telling me that I can't do something that I should be able to do. Like
if I got an error message for the line ”If a=b Then”, and the error
message says: ”BASIC syntax error: a and b are already compared”…
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Laurent Godard
2011-08-18 06:52:32 UTC
Permalink
HI

from my poor experience
if I want to use redim, i have to define the array as dynamic

may be try this

public NDice() as integer

sub main
redim NDice(1 to 6)

end sub

Laurent
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Johnny Rosenberg
2011-08-18 09:20:11 UTC
Permalink
Post by Laurent Godard
HI
from my poor experience
if I want to use redim, i have to define the array as dynamic
may be try this
public NDice() as integer
sub main
 redim NDice(1 to 6)
end sub
Laurent
--
That is probably a good idea in most cases, but it gives the same
error if the array is defined in another module (I just tried it in my
example).
I have worked around it now in my project (I found that I could just
move the initial definition to the same module where the ReDim
statement is, which works for this project but not in all cases), but
I still want to know if this is a bug or not, so I can report it if it
is.


Thanks for your input, you are the only one who replied, so I guess
most people hate me for all those annoying questions I posted
recently… ;D


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Stephan Bergmann
2011-08-18 11:58:34 UTC
Permalink
Post by Johnny Rosenberg
Thanks for your input, you are the only one who replied, so I guess
most people hate me for all those annoying questions I posted
recently… ;D
I guess its rather the move to Apache OpenOffice that cut down the activity on this mailing list... Don't take it personal. :)

-Stephan--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Johnny Rosenberg
2011-08-18 12:33:11 UTC
Permalink
Post by Johnny Rosenberg
Thanks for your input, you are the only one who replied, so I guess
most people hate me for all those annoying questions I posted
recently… ;D
I guess its rather the move to Apache OpenOffice that cut down the activity on this mailing list...  Don't take it personal.  :)
Well, I'll try not to… ;D

Do you happen to know of a list for things like this with more
activity? LibreOffice didn't seem to have a similar list either.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ
-Stephan--
-----------------------------------------------------------------
with Subject: help
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Stephan Bergmann
2011-08-18 13:35:02 UTC
Permalink
Post by Johnny Rosenberg
Do you happen to know of a list for things like this with more
activity? LibreOffice didn't seem to have a similar list either.
No, don't know a better list for now. Most traffic on ooo-***@incubator.apache.org currently is about moving the project, not about using or hacking the project. In the end, the goal is to resurrect the OOo infrastructure over at Apache, if not as an exact clone then at least in spirit, so it might be a good idea to stick to this list for the moment---even if you sometimes have to write the answers to your questions yourself… ;)

-Stephan--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help

Johnny Rosenberg
2011-08-17 13:43:04 UTC
Permalink
Post by Johnny Rosenberg
I edited a lot of code yesterday, one of the rows I did NOT edit was
ReDim NDice(1 To 6) As Integer
Public NDice(1 To 6) As Integer
The NDice variable is already defined.”
Yes, I know it is, that's one of the points by using ReDim, isn't it?
Before doing some heavy editing there was no complaints on this line
at all, and as I said, this is NOT one of the many lines I changed.
So obviously some of my other edits caused this, but where to start
looking? What can I possible have done to cause this?
I just hate when these things happen, when there is an error message
telling me that I can't do something that I should be able to do. Like
if I got an error message for the line ”If a=b Then”, and the error
message says: ”BASIC syntax error: a and b are already compared”…
I have now tried a couple of more things. I have changed the global
definition from Public to Private to Global to Dim and back again to
Public. Same result no matter what.
I have even removed the global definition, and I have searched through
ALL the modules after the variable to make sure it's not declared
elsewhere. STILL the error message says that it's already defined! I
would like to know where, because the Find thing (Ctrl+f) can't find
another definition of NDice than the ReDim on which the error occurs!
I have searched for Dim NDice, Public NDice, Private NDice and Global
NDice, nothing found except ReDim NDice (once – the one on which the
error occurs).
Well, I'll continue searching. I have now made a copy of the whole
document for wild debugging, so I will try the most brutal things I
can think of now, like removing entire modules to see if I can at
least find in what module I need to have a closer look at…
Any ideas are very welcome! I'm kind of losing my hair here… :P
I have now found a line which seems to cause the problem. The
confusing thing is that I can't really see why, because it doesn't
seem to have anything to do with anything (except that the problem
disappears when the line is out commented or removed).

However, now I can at least reproduce the problem without including
thousand of lines of code. Here's a simple example showing what I
mean:

Open OpenOffice.org Calc or LibreOffice Calc (I tested LibreOffice
Calc 3.3.3 on Ubuntu 10.10 and OpenOffice.org Calc 3.2.0 on Ubuntu
10.04, same results).
Click your way to the BASIC IDE (Tools → Macro → OpenOffice.org Basic
(or LibreOffice Basic) → Untitled 1 → New → Accept the given name →
OK.
Create a new module, accept the given name.
Now you have two modules, right? Module1 and Module2. Actually you
only need one to discover this behaviour, but I think two modules
illustrates it better, perhaps…
In module 2, replace what's there with the following:
REM ***** BASIC *****

Option Explicit

Public a(1 To 6) As Integer


Sub Main
Print "This is not important in this case."
End Sub

In module 1, replace what's there with the following:
REM ***** BASIC *****

Option Explicit


Sub Main
a(1)=2
End Sub

Sub Test
ReDim a(1 To 6) As Integer
Dim i As Integer

For i=1 To 6
a(i)=i
Next i
Print "a(4)= " & a(4)
End Sub

Now try to run one of the subroutines in module 1.
You'll get an error message at ”ReDim a(1 To 6) As Integer” no matter
which one of the subroutines you are trying to run.
Now, replace the main subroutine in Module1 with:
Sub Main
REM a(1)=2
End Sub

You can now run both subroutines without any problems (except that
Main doesn't do anything, but there will be no Error messages).

Here's another trick: Replace all text in Module 1 with:
REM ***** BASIC *****

Option Explicit



Sub Test
ReDim a(1 To 6) As Integer
Dim i As Integer

For i=1 To 6
a(i)=i
Next i
Print "a(4)= " & a(4)
End Sub


Sub Main
a(1)=2
End Sub


It's the same code as before, but Man and Test now switched places.
Run one of the subroutine. No error messages.

So the big question (at least for me): WHY?
Why does ReDim give an error just because the same array is used in a
subroutine written above? A subroutine that has no connection to the
other one, what so ever, more than it is using the same array…
This has to be a bug, right? If not, what is the thought behind it?

At least I know a couple of possible solutions to my problem now:
1. Move the troubling subroutine so it becomes the first one in the module.
2. Don't use ReDim if not really necessary, like in this case. I used
it to reset all elements to 0 very quickly. I can do that with a For
loop as well, even if it takes much longer time. In this case it's not
a problem since there are only 6 elements, but what about if there are
thousands of elements and resetting needs to be done in a loop that is
run thousands of times…?
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Johnny Rosenberg
2011-08-17 16:21:20 UTC
Permalink
So here I am again, having fun by sending messages to myself…
Post by Johnny Rosenberg
Post by Johnny Rosenberg
I edited a lot of code yesterday, one of the rows I did NOT edit was
ReDim NDice(1 To 6) As Integer
Public NDice(1 To 6) As Integer
The NDice variable is already defined.”
Yes, I know it is, that's one of the points by using ReDim, isn't it?
Before doing some heavy editing there was no complaints on this line
at all, and as I said, this is NOT one of the many lines I changed.
So obviously some of my other edits caused this, but where to start
looking? What can I possible have done to cause this?
I just hate when these things happen, when there is an error message
telling me that I can't do something that I should be able to do. Like
if I got an error message for the line ”If a=b Then”, and the error
message says: ”BASIC syntax error: a and b are already compared”…
I have now tried a couple of more things. I have changed the global
definition from Public to Private to Global to Dim and back again to
Public. Same result no matter what.
I have even removed the global definition, and I have searched through
ALL the modules after the variable to make sure it's not declared
elsewhere. STILL the error message says that it's already defined! I
would like to know where, because the Find thing (Ctrl+f) can't find
another definition of NDice than the ReDim on which the error occurs!
I have searched for Dim NDice, Public NDice, Private NDice and Global
NDice, nothing found except ReDim NDice (once – the one on which the
error occurs).
Well, I'll continue searching. I have now made a copy of the whole
document for wild debugging, so I will try the most brutal things I
can think of now, like removing entire modules to see if I can at
least find in what module I need to have a closer look at…
Any ideas are very welcome! I'm kind of losing my hair here… :P
I have now found a line which seems to cause the problem. The
confusing thing is that I can't really see why, because it doesn't
seem to have anything to do with anything (except that the problem
disappears when the line is out commented or removed).
However, now I can at least reproduce the problem without including
thousand of lines of code. Here's a simple example showing what I
Open OpenOffice.org Calc or LibreOffice Calc (I tested LibreOffice
Calc 3.3.3 on Ubuntu 10.10 and OpenOffice.org Calc 3.2.0 on Ubuntu
10.04, same results).
Click your way to the BASIC IDE (Tools → Macro → OpenOffice.org Basic
(or LibreOffice Basic) → Untitled 1 → New → Accept the given name →
OK.
Create a new module, accept the given name.
Now you have two modules, right? Module1 and Module2. Actually you
only need one to discover this behaviour, but I think two modules
illustrates it better, perhaps…
Just found out that we need two modules to illustrate the problem,
more on that further down…
Post by Johnny Rosenberg
REM  *****  BASIC  *****
Option Explicit
Public a(1 To 6) As Integer
Sub Main
       Print "This is not important in this case."
End Sub
REM  *****  BASIC  *****
Option Explicit
Sub Main
       a(1)=2
End Sub
Sub Test
       ReDim a(1 To 6) As Integer
       Dim i As Integer
       For i=1 To 6
               a(i)=i
       Next i
       Print "a(4)= " & a(4)
End Sub
Now try to run one of the subroutines in module 1.
You'll get an error message at ”ReDim a(1 To 6) As Integer” no matter
which one of the subroutines you are trying to run.
Sub Main
REM     a(1)=2
End Sub
You can now run both subroutines without any problems (except that
Main doesn't do anything, but there will be no Error messages).
REM  *****  BASIC  *****
Option Explicit
Sub Test
       ReDim a(1 To 6) As Integer
       Dim i As Integer
       For i=1 To 6
               a(i)=i
       Next i
       Print "a(4)= " & a(4)
End Sub
Sub Main
       a(1)=2
End Sub
It's the same code as before, but Main and Test now switched places.
Run one of the subroutine. No error messages.
I found that moving the Public definition from Module2 to Module1 also
solves the problem, but I guess the problem could occur in Module2
instead, depending on what the code looks like there. I can imagine
that there could be a situation where problems are almost unavoidable,
even though it didn't happen to me yet…

Maybe I have ten functions and/or subroutines in Module1 and Module 2
of which all of them use that variable and all of them needs the ReDim
command…
Post by Johnny Rosenberg
So the big question (at least for me): WHY?
Why does ReDim give an error just because the same array is used in a
subroutine written above? A subroutine that has no connection to the
other one, what so ever, more than it is using the same array…
This has to be a bug, right? If not, what is the thought behind it?
1. Move the troubling subroutine so it becomes the first one in the module.
2. Don't use ReDim if not really necessary, like in this case. I used
it to reset all elements to 0 very quickly. I can do that with a For
loop as well, even if it takes much longer time. In this case it's not
a problem since there are only 6 elements, but what about if there are
thousands of elements and resetting needs to be done in a loop that is
run thousands of times…?
3. Move the global definition from Module2 to Module1 also solves the
problem, but what about if you need to do a ReDim in Module2 if a
subroutine or function above the one with the ReDim also use that
variable?
4. Don't use external variables – I found this hard to do though,
especially when using custom dialogues.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ
--
-----------------------------------------------------------------
To unsubscribe send email to dev-***@api.openoffice.org
For additional commands send email to ***@api.openoffice.org
with Subject: help
Loading...