Re: ReDim driving me crazy again…
Johnny Rosenberg <[email protected]> Wed, 17 Aug 2011 18:21:20 +0200
| Newsgroups | gmane.comp.openoffice.devel.api |
|---|---|
| Message-ID | <CADo7T4eE8yU5FZiuCkRZKLLa1VCfYhpaR47QaVoe5j_fTepNKw@mail.gmail.com> |
So here I am again, having fun by sending messages to myself=E2=80=A6 2011/8/17 Johnny Rosenberg <[email protected]>: > 2011/8/17 Johnny Rosenberg <[email protected]>: >> 2011/8/17 Johnny Rosenberg <[email protected]>: >>> 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): >>> =E2=80=9DBASIC syntax error: >>> The NDice variable is already defined.=E2=80=9D >>> >>> 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 =E2=80=9DIf a=3Db Then=E2=80=9D,= and the error >>> message says: =E2=80=9DBASIC syntax error: a and b are already compared= =E2=80=9D=E2=80=A6 >>> >> >> 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 =E2=80=93 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=E2=80=A6 >> >> Any ideas are very welcome! I'm kind of losing my hair here=E2=80=A6 :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 =E2=86=92 Macro =E2=86=92 OpenOffi= ce.org Basic > (or LibreOffice Basic) =E2=86=92 Untitled 1 =E2=86=92 New =E2=86=92 Accep= t the given name =E2=86=92 > 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=E2=80=A6 Just found out that we need two modules to illustrate the problem, more on that further down=E2=80=A6 > In module 2, replace what's there with the following: > REM =C2=A0***** =C2=A0BASIC =C2=A0***** > > Option Explicit > > Public a(1 To 6) As Integer > > > Sub Main > =C2=A0 =C2=A0 =C2=A0 =C2=A0Print "This is not important in this case." > End Sub > > In module 1, replace what's there with the following: > REM =C2=A0***** =C2=A0BASIC =C2=A0***** > > Option Explicit > > > Sub Main > =C2=A0 =C2=A0 =C2=A0 =C2=A0a(1)=3D2 > End Sub > > Sub Test > =C2=A0 =C2=A0 =C2=A0 =C2=A0ReDim a(1 To 6) As Integer > =C2=A0 =C2=A0 =C2=A0 =C2=A0Dim i As Integer > > =C2=A0 =C2=A0 =C2=A0 =C2=A0For i=3D1 To 6 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a(i)=3Di > =C2=A0 =C2=A0 =C2=A0 =C2=A0Next i > =C2=A0 =C2=A0 =C2=A0 =C2=A0Print "a(4)=3D " & a(4) > End Sub > > Now try to run one of the subroutines in module 1. > You'll get an error message at =E2=80=9DReDim a(1 To 6) As Integer=E2=80= =9D no matter > which one of the subroutines you are trying to run. > Now, replace the main subroutine in Module1 with: > Sub Main > REM =C2=A0 =C2=A0 a(1)=3D2 > 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 =C2=A0***** =C2=A0BASIC =C2=A0***** > > Option Explicit > > > > Sub Test > =C2=A0 =C2=A0 =C2=A0 =C2=A0ReDim a(1 To 6) As Integer > =C2=A0 =C2=A0 =C2=A0 =C2=A0Dim i As Integer > > =C2=A0 =C2=A0 =C2=A0 =C2=A0For i=3D1 To 6 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a(i)=3Di > =C2=A0 =C2=A0 =C2=A0 =C2=A0Next i > =C2=A0 =C2=A0 =C2=A0 =C2=A0Print "a(4)=3D " & a(4) > End Sub > > > Sub Main > =C2=A0 =C2=A0 =C2=A0 =C2=A0a(1)=3D2 > 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=E2=80=A6 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=E2=80=A6 > 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=E2=80=A6 > 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 modul= e. > 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=E2=80=A6? 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 =E2=80=93 I found this hard to do though, especially when using custom dialogues. Kind regards Johnny Rosenberg =E3=82=B8=E3=83=A7=E3=83=8B=E3=83=BC=E3=83=BB=E3=83=AD=E3=83=BC=E3=82=BC=E3= =83=B3=E3=83=90=E3=83=BC=E3=82=B0 --=20 ----------------------------------------------------------------- To unsubscribe send email to [email protected] For additional commands send email to [email protected] with Subject: help