Workaround to include C# 7 features
Workaround to include C# 7 features
27 May 2019, 06:26
I've found a very old thread (https://ctrader.com/forum/cbot-support/9462) that claims there is a workaround to use a newer version of .NET to build cBots for cAlgo. I've tried in Visual Studio, but are unable to build anything that uses C# language features above C# 4.
Can anyone confirm if they have managed to do this as detailed in that thread? Or is it a pipe dream? I'd love to use some of the modern C# features, .NET 4 is ancient and getting restrictive.
Spotware, that thread is 3 years old now, can you give us any indication of support for a newer .NET version, please?
Thanks
Replies
mpistorius
29 May 2019, 10:57
Hi Paul,
Thanks for the reply. I tried setting the application target framework in my VS cBot project to .NET 4.6 and in the advanced build settings, I set the language version to C# 6. But in doing that I am still not able to compile valid C# 6 code. For example, trying to compile something like
using static cAlgo.Utils;
results in an error when building the .algo file:
error: Identifier expected; 'static' is a keyword error: Expected class, delegate, enum, interface, or struct
Similarly, I can't use expression body declarations like
public void Invalidate() => invalidated = true; error: ; expected error: Invalid token '=' in class, struct, or interface member declaration
That's why I assumed C# 6 is not supported and I'm stuck with C# 4
Not sure if it is related, but I'm using a workaround to run the cBot extension in Visual Studio 2019. Perhaps that breaks something as VS 2017 is officially supported, but Spotware provided a workaround recently in the forums.
Can you tell me what I need to do to use C# 6 when building my cBots, please? Sorry if this is a noob question, I come from a C++ background and only just started converting Metatrader EAs to C#
Thanks
@mpistorius
ClickAlgo
29 May 2019, 17:49
Please send a basic example VS project to development@clickalgo.com and I will get someone to take a look for you, just a few lines of code using a C# 6 feature.
Also. double check that the features you are attempting to use are available with .NET 4.6
Paul
@ClickAlgo
mpistorius
29 May 2019, 18:36
Hi Paul,
Again, thank you so much for your willingness to lend a hand. I'll send you a very basic VS project that uses the two C# 6 features I mentioned above, and for the sake of others reading this thread, I'll post sample code here. The two features I tested were using static directive (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static) and expression-bodied members (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members), both were introduced in C# 6. Here is a simple example (this builds fine in VS 2019 if built as a standard C# dll, but won't build with the cBot extension into an .algo file. Neither will it build straight from the cTrader Automate tab.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; using static cAlgo.Robots.MyStatic; // <-- Doesn't compile in cTrader namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class TestCS6 : Robot { private int counter = 0; public int TickCount => counter++; // <-- Doesn't compile in cTrader protected override void OnStart() { Print( AddOne( 10 ) ); } protected override void OnTick() { Print( TickCount ); } } public static class MyStatic { public static int AddOne( int n ) { return n + 1; } } }
Thanks again,
Morné
@mpistorius
bishbashbosh
15 Oct 2019, 21:55
( Updated at: 21 Dec 2023, 09:21 )
Ping on this one - the following build output is on cT 3.6b, compiling in VS 2019 Community:
@bishbashbosh
afhacker
16 Oct 2019, 00:15
Put your code on a separate class library project and compile it with Visual Studio, then reference the compiled library to your indicator/cBot.
That's how you can use any of the new C# features but if you put your code inside indicator/cBot project then for compiling cTrader compiler will be used which will fail as it doesn't support the new features.
@afhacker
bishbashbosh
23 Oct 2019, 15:21
RE:
afhacker said:
Put your code on a separate class library project and compile it with Visual Studio, then reference the compiled library to your indicator/cBot.
That's how you can use any of the new C# features but if you put your code inside indicator/cBot project then for compiling cTrader compiler will be used which will fail as it doesn't support the new features.
I see. So the choice is either all-in-one solution or new C# - gotcha.
@bishbashbosh
afhacker
23 Oct 2019, 21:15
RE: RE:
bishbashbosh said:
afhacker said:
Put your code on a separate class library project and compile it with Visual Studio, then reference the compiled library to your indicator/cBot.
That's how you can use any of the new C# features but if you put your code inside indicator/cBot project then for compiling cTrader compiler will be used which will fail as it doesn't support the new features.
I see. So the choice is either all-in-one solution or new C# - gotcha.
You can put all the projects on a single solution, you don't have to create separate solutions unless you have to.
@afhacker
berkninan
07 Jan 2020, 07:02
It will help you to know more about..C# versions and features
@berkninan
JerryTrader
20 Jan 2021, 21:22
RE: RE: RE:
afhacker said:
bishbashbosh said:
afhacker said:
Put your code on a separate class library project and compile it with Visual Studio, then reference the compiled library to your indicator/cBot.
That's how you can use any of the new C# features but if you put your code inside indicator/cBot project then for compiling cTrader compiler will be used which will fail as it doesn't support the new features.
I see. So the choice is either all-in-one solution or new C# - gotcha.
You can put all the projects on a single solution, you don't have to create separate solutions unless you have to.
Hi,
Thanks for your tip, it works find, but do you have any solution to simply build everything just using Ctrl + Shift + B (Build Solution) ?
Currently, I need to individually build each DLL before building the solution (which will call the cTrader compiler).
This is a lot of intermediate steps which can lead to some errors (or just forgot to build the DLL since we are used to only "Build Solution").
Thanks !
@JerryTrader
ClickAlgo
29 May 2019, 08:59
Hi, Pistorius,
We have been using .NET Framework 4.6 and 4.7 for a while now with the core cBots using C# 6, I think C# 7 uses .NET core, I am pretty sure that .NET 4.7 came out in October 2017 and it still quite new. Unless there are features that you really need in C#7 I would just use C#6
NET Framework History
NET Core History
Paul Hayes
Sales & Marketing
Email: contact@clickalgo.com
Phone: (44) 203 289 6573
Website: https://clickalgo.com
Twitter | Facebook | YouTube | Pinterest | LinkedIn
@ClickAlgo