So here I am again, having fun by sending messages to myself…

2011/8/17 Johnny Rosenberg <gurus.knu...@gmail.com>:
> 2011/8/17 Johnny Rosenberg <gurus.knu...@gmail.com>:
>> 2011/8/17 Johnny Rosenberg <gurus.knu...@gmail.com>:
>>> 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”…
>>>
>>
>> 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…

Just found out that we need two modules to illustrate the problem,
more on that further down…

> 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 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…

> 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…?

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-unsubscr...@api.openoffice.org
For additional commands send email to sy...@api.openoffice.org
with Subject: help

Reply via email to