The “using static” directive
The “using static” directive
29 Jan 2018, 00:17
In Visual Studio, I use the “using static” directive in other solutions without any problems: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static. However, I cannot get the “using static” directive (as shown in the following 2 lines of code) to work in my cAlgo solution. Does anyone know if this is to do with cAlgo and how to fix it?
CODE IN VS
using static cAlgo.ENUM_BOOK_TYPE; // MQL5 using static cAlgo.Global;
VS ERROR LIST
Thanks,
Replies
Daniel D.
30 Jan 2018, 15:32
Hi,
To test the "using static" directive , the following code shows 2 versions of the same example. For anyone who would prefer not to use the message box, then replace it with a Print(..) statement and limit the AccessRights, but, for those who want to use the message box, first a reference to the “System.Windows.Forms” assembly (see the Solution Explorer).
(Version 1) The first version will compile with cAlgo, but the source code requires the static method's and property's names to be qualified with their class name (Global).
(Version 2) The second version (further below) is more succinct and I think it's neater, and it is compliant with C# and .NET. But, it will not compile with cAlgo. If you try to compile it, note that cAlgo's compiler does not even recognise the “using static” directive. Maybe the “using static” directive needs to be added in the next release of cAlgo: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
Thanks,
Daniel
Version 1
using System; using cAlgo.API; //using static cAlgo.Global; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class MyFirst_cBot : Robot { protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { if (DateTime.Now.AddSeconds(50) > Global.DateTimeOfNewsEvent) { Global.DateTimeOfNewsEvent = DateTime.MaxValue; PrepareForNewEvent(); } } private void PrepareForNewEvent() { // Close positions. System.Windows.Forms.MessageBox.Show( "Posistions closed due to pending news event:\r\n\r\n\t" + Global.DateTimeOfNewsEvent.ToString("HH.mm.ss") + " — " + Global.NewsEventDescription , "NEWS EVENT"); } } public static class Global { private static object mutex = new object(); private static string newsEventDescription = "Gross Domestic Product, both (QoQ) and (YoY)"; public static string NewsEventDescription { set { lock (mutex) { newsEventDescription = value; } } get { lock (mutex) { return newsEventDescription; } } } private static DateTime dtNewsEvent = DateTime.Now.AddMinutes(1); public static DateTime DateTimeOfNewsEvent { set { lock (mutex) { dtNewsEvent = value; } } get { lock (mutex) { return dtNewsEvent; } } } } }
Version 2
using System; using cAlgo.API; using static cAlgo.Global; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class MyFirst_cBot : Robot { protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { if (DateTime.Now.AddSeconds(50) > DateTimeOfNewsEvent) { DateTimeOfNewsEvent = DateTime.MaxValue; PrepareForNewEvent(); } } private void PrepareForNewEvent() { // Close positions. System.Windows.Forms.MessageBox.Show( "Posistions closed due to pending news event:\r\n\r\n\t" + DateTimeOfNewsEvent.ToString("HH.mm.ss") + " — " + NewsEventDescription , "NEWS EVENT"); } } public static class Global { private static object mutex = new object(); private static string newsEventDescription = "Gross Domestic Product, both (QoQ) and (YoY)"; public static string NewsEventDescription { set { lock (mutex) { newsEventDescription = value; } } get { lock (mutex) { return newsEventDescription; } } } private static DateTime dtNewsEvent = DateTime.Now.AddMinutes(1); public static DateTime DateTimeOfNewsEvent { set { lock (mutex) { dtNewsEvent = value; } } get { lock (mutex) { return dtNewsEvent; } } } } }
@Daniel D.
Daniel D.
30 Jan 2018, 15:41
RE:
The method should be called private void PrepareForNewEvent()@Daniel D.
Daniel D.
30 Jan 2018, 15:47
RE: RE:
This is the first time that I have posted to this website. How do you update or delete a post?@Daniel D.
PanagiotisCharalampous
31 Jan 2018, 15:36
Hi Daniel,
The reason you get these errors is because this a feature of C# 6.0. cAlgo compiler supports features up to C# 4.0.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
31 Jan 2018, 17:05
Hi MaRCHeW,
Yes we have plans doing this in one of the future versions.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
29 Jan 2018, 12:38
Hi Daniel,
Thanks for posting in our forum. Can you please share your code so that we can check what is wrong?
Best Regards,
Panagiotis
@PanagiotisCharalampous