Method is too long ?????

Got a really strange error.

I have a method that saves a lot of data to a file.
It has 1335 lines and when compiling I get the error

This method is too long
End Function

Why is this happening? I mean , I have methods that have more then 3000 lines and I do not get this error.

wow. Maybe too many local variables due to a lot of expressions?

About 3 local variables, one for-next loop and 10 if then
All other code is like this:

line = line + "<key>Audio1"+str(y)+" "+str(batchlist(batchID).encodefilterObjects(y).audio1)+"</key>"+chr(13)
line = line + " <false/>"+chr(13)

A total of 1335 lines. If I reduce it to 1300 lines it works fine.

Now:

  • Putting a limit on a method is strange - so that should give an error at all
  • Other methods with more code lines do not trigger this error. I even have one with more then 3000 lines.

Strange…

if I’m correct every + here will make an expression and make a local variable for the temp result.
Maybe simply split in two methods?

OK, I got it working with a work-a-round:

When I use this:

For y as integer = 0 to batchlist(batchID).encodefilterObjects.ubound line = line + "<key>Audio1"+str(y)+" "+str(batchlist(batchID).encodefilterObjects(y).audio1)+"</key>"+chr(13) line = line + " <false></false>"+chr(13) .... (about 100 lines in this for-next loop) next y

-> this gives a compiler error.
This method is too long
End Function

When I replace the for-nextloop with a while-wend loop it is working fine.

Dim y as integer = 0 while y-1 < batchlist(batchID).encodefilterObjects.ubound line = line + "<key>Audio1"+str(y)+" "+str(batchlist(batchID).encodefilterObjects(y).audio1)+"</key>"+chr(13) line = line + " <false></false>"+chr(13) .... (about 100 lines in this for-next loop) y = y +1 Wend

So this is definitely a (strange) bug.

And even more strange.

I have the same method but now for loading the saved settings from the above method.
That method has the SAME amount of code lines and does not give the error.

Bummer. :slight_smile:

[quote=122625:@Christoph De Vocht]Got a really strange error.

I have a method that saves a lot of data to a file.
It has 1335 lines and when compiling I get the error

This method is too long
End Function

Why is this happening? I mean , I have methods that have more then 3000 lines and I do not get this error.[/quote]

Yeah dont do that

There can be a lot of temporary variables created and once you do that you can run out of space on the stack frame for locals temporaries etc

Thats what the compiler is telling you

Recall I’ve seen this method before & that would be about the first thing I would do
Split it into several methods rather than one giant one

The troubling methode is just using 3 local variables.
One of the local variable just add text to a string variable. Nothing more nothing less. I would say about 100 lines, so that isn’t much either.

And why does it work when I put the same code between while-wend loop?

Do you run into this error in 2014r2.1?

Did a quick test:
Got the error in both 2014r2.1 and 2014r3a9

Another test:
When I remove some lines so the for-next loop only has about 80 lines the compiler does not complain.

To be clear:
The For-next loop only has code like this:

line = line + “Audio1”+str(y)+" “+str(batchlist(batchID).encodefilterObjects(y).audio1)+”"+chr(13)
line = line + " "+chr(13)

batchlist(batchID).encodefilterObjects(y).audio1 is an integer and mostly between 1 or 5 chars long.
So in the end the line.len would be between 100 and 500. Nothing special I guess.

Since it’s not new to the alpha, I’m moving this into general.

And did some more tests:

It seems it doesn’t have to do anything with methods at all.
There is a limit in code lines between for-next loops which is just crazy imo
On first tests a max. of 375 lines is allowed.

Now isn’t that very strange?

To reproduce:

Add the below code in the Open event of a window:

[code]Dim text as string
for a as integer = 0 to 100
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)
text = text + “This is a test”+chr(13)

… copy/paste the text = text + “This is a test”+chr(13) a couple of times

text = text + "This is a test"+chr(13)
text = text + "This is a test"+chr(13)
text = text + "This is a test"+chr(13)

next a
[/code]
This will give an error:

This method is too long
End Sub

Putting the same code between do-loop or while-wend works fine.

Do I need to file in feedback case?

[quote=122670:@Christoph De Vocht]line = line + “Audio1”+str(y)+" “+str(batchlist(batchID).encodefilterObjects(y).audio1)+”"+chr(13)
line = line + " "+chr(13)[/quote]

You can probably save a lot of temporary variables shuffle with continued lines such as :

[code]line = line + “Audio1”+str(y)+" “+str(batchlist(batchID).encodefilterObjects(y).audio1)+”"+chr(13) _

  • " "+chr(13)[/code]

Each time you go line = line+, I believe that creates in effect two temporary variables on top of the original line to hold the result, and the concatenating one. If for some reason the stack is not releasing them right away, it can probably fill it up real quick.

In terms of code formatting, a continued line will probably not shatter too much the presentation, and may save you some extra memory.

Yes, that would help for sure. But still … it is a strange issue.
It also does not explain why it works in other loops like do-loop and while-wend. :slight_smile:

[quote=122680:@Christoph De Vocht]There is a limit in code lines between for-next loops which is just crazy imo
On first tests a max. of 375 lines is allowed.[/quote]
This is nonsense. I have for-next loops in my apps with way more than 1000 lines in between.

You should - as Norman has indicated - refactor your code to not create as many temporary variables as now.
Create functions for building parts of the final string. I assume you are creating a plist-like string. Then you could create methods like:

Function CreateBooleanEntry(key As String, value As Boolean) As String Function CreateIntegerEntry(key As String, value As Integer) As String ...

[quote=122683:@Christoph De Vocht]Yes, that would help for sure. But still … it is a strange issue.
It also does not explain why it works in other loops like do-loop and while-wend. :-)[/quote]

Norman suggested it is a memory issue, and I will trust his expertise.

I am not a system designer, but I would believe a while-wend or do-loop require less memory than a for next since they only verify a condition as opposed to keep a counter. But without being your code, I cannot help to wonder if having so many lines in a method does not indicate a need for breaking it in smaller methods. The advantage of a separate method is also that it releases memory after termination.

Did you tried the example code above? When more then 1000 lines it shows the error.

Try appending them into an array, and use JOIN at the end…?

Each one of those generates several temporaries

I referred explicitly to the following conclusion you made - which is nonsense - not to your example:[quote]Christoph De Vocht There is a limit in code lines between for-next loops which is just crazy imo. On first tests a max. of 375 lines is allowed.[/quote]
I also gave an example how you could reduce the memory foot print by splitting your sting generation into smaller parts.