Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

drawback of declaring ENUM's inside class?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    drawback of declaring ENUM's inside class?

    I've seen that most people recommends to create a new namespace and pub enums inside it each individual indicator/strategy, like:

    Code:
    ...........
    using MyStrategy_enums;
    namespace MyStrategy_enums{
       public enum XYZ{
         X,
         Y,
         Z
       } 
    }
    .....
    I wonder, why we are creating a new namespace, while we can do it in a more compact way like:

    case 1:
    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here 
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class MyStrategy: Strategy
        {
           
           public enum XYZ{
                ....
                .... 
           }
           ...................
    question A) will that come into conflict if in other strategy, there is same name'd enums? question B) does this way will pollute global namespace of `Strategy` (there is colon after Strategy's name, so, it shouldnt pollute, right)?

    or even, case 2:
    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here 
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class MyStrategy: Strategy
        {
           
           public class MyStrategy_enums{
                  public enum first{
                      .....
                  }
                  public enum second{
                      .....
                  }
           }
           ...................
    
           ///and referring with:
           MyStrategy_enums.first


    why enums using Namespaces are preferred over CASE-1 or CASE-2?
    Last edited by ttodua; 09-11-2017, 02:56 PM.

    #2
    Hello selnomeria,

    Thanks for opening the thread.

    Choosing where to place the enum would impact how you you would want to be able to access and use it. Let's pretend we are using this in an indicator for the remainder of this post as an example.

    If you create an enum and use it to in a parameter, you may want to use a separate namespace so any NinjaScript that uses this indicator can add a directive for that namespace so it can use the enum for the indicator parameter.

    If you have an enum that has no external purpose in the indicator, I would advise to keep it central to the class that uses it like you have described in case 1 and case 2.

    Please let me know if I may be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      please tell me if i am right:
      1) yes, i use that as a paremeter in the same indicator!
      so, if i correctly understand, when i use that ENUM in parameter for the same indicator/strategy, then i can use any of those 3 variations, right?

      2) but if i want to call that indicator from other script (with it's enum parameters to be choosable), then only NAMESPACE will help in that case, right? if so, then yes, we should ALWAYS call it with namespaces... but please confirm - if we use CASE-1 or CASE-2, then if we call this indicator from other indicators, then we wont be able to set that enums ?
      Last edited by ttodua; 09-11-2017, 03:34 PM.

      Comment


        #4
        Hello selnomeria,

        These questions are very related, I'll answer briefly and then elaborate.

        1) - Not quite.

        2) - Your understanding is correct here.

        If the enum is for a parameter, and you define the enum within the class of the indicator, then another calling NinjaScipt will not know what the enum is. If you make it in a namespace, then you can add a using directive for that namespace, and then use the enum normally.

        If you have any additional questions, please don't hesitate to ask.
        JimNinjaTrader Customer Service

        Comment


          #5
          if you create a seperate indicator just for enums it works great. if you are going to create many types of indicators that will use the same enums, they will be grouped there. I created 4 indicators that require the same enum and all I do is call the enum indicator i made. I have no issues with dlls and globalization of enums anymore. One key is to put Name = string.Empty; in the initializaton in order to hide the indicator from the user but it will still be accessed for you indicator.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sidlercom80, 10-28-2023, 08:49 AM
          166 responses
          2,234 views
          0 likes
          Last Post sidlercom80  
          Started by thread, Yesterday, 11:58 PM
          0 responses
          1 view
          0 likes
          Last Post thread
          by thread
           
          Started by jclose, Yesterday, 09:37 PM
          0 responses
          6 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,414 views
          0 likes
          Last Post Traderontheroad  
          Started by firefoxforum12, Yesterday, 08:53 PM
          0 responses
          11 views
          0 likes
          Last Post firefoxforum12  
          Working...
          X