0

My question is to how to split into multiple files a method containing an override?

I understand that this is not possible with partial.

In my code, I have too much lines in this method. I code on the QuantConnect platform that limits the size of one file, and I reach this limit.

public partial class TestAlgo : QCAlgorithm
{
    public override void OnData(Slice slice) // Name OnData can't be modified.  
    {
    Indices:
        {
            //First Indices
        }

        {
            //Second Indices
        }
        ...
    }
}
orad
  • 15,272
  • 23
  • 77
  • 113
  • 5
    You can't split a method over files. If you find your method is too complex / large / long it is an indication that you should re-design your method. How you refactor depends on many factors, it could be splitting code up until multiple methods or class redesign or something else (there is no one shoe fits all solution). – Igor Oct 07 '19 at 15:50
  • Looks like you have a lot of _data_, not code. Can you read the data from a file? – CodeCaster Oct 07 '19 at 16:31
  • No, data are provided by the platform. – Gmamuze Cht Oct 07 '19 at 16:35
  • Can you show a snippet of what your code actually looks like then? I can't imagine having that many lines of code. – CodeCaster Oct 08 '19 at 08:00
  • I cant show more srry, since yestersday i used derived class but the runcore (Lean) of quantconnect doesn't like to have multiple derived name :'( While the compiler doesn't show error. – Gmamuze Cht Oct 08 '19 at 13:12
  • Looks like, for each index, you have written similar code. And you have more such indices, so the code is so big. If we get some pattern we can help. – T.kowshik Yedida Jun 08 '21 at 03:18

2 Answers2

4

Unfortunately, this is not possible. You should refactor your code so that you split your method into multiple methods instead, and then you could use partial, though I'm still not sure why you'd want to do that rather than refactoring complex code into multiple classes.

Mark Cilia Vincenti
  • 1,410
  • 8
  • 25
0

It says right there in the documentation:

Partial methods are implicitly private, and therefore they cannot be virtual.

More to the point, even with non-virtual methods, partial doesn't allow you to split the method body itself across multiple files. It's just a way of allowing one file to declare the method and another to provide the implementation.

So, we should focus on this part of your question, rather than the XY Problem you've asked about:

I have too much lines in this method. I code on the QuantConnect platform that limits the size of one file, and I reach this limit.

Whatever the limit of the size of the file, I would guess it's a reasonably generous limit. If you've reached that limit as a result of a single method, then you have way too much code in that method.

There are lots of guidelines about how many lines of code a method should have. They are fairly subjective in nature. People debate whether it's "one screen" or "two screens" or something else. But it's safe to say that you've gone way beyond this.

Your method really needs to be refactored into smaller pieces. Probably into smaller classes. How exactly to do that, can't be answered here because you haven't provided that context. But, it needs to be done.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136