• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 TazoTodua; 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 TazoTodua; 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 fredfred, Today, 03:41 AM
          2 responses
          7 views
          0 likes
          Last Post fredfred  
          Started by TraderYoda, Today, 03:33 AM
          0 responses
          1 view
          0 likes
          Last Post TraderYoda  
          Started by pjsmith, Today, 02:56 AM
          0 responses
          2 views
          0 likes
          Last Post pjsmith
          by pjsmith
           
          Started by ntbone, Yesterday, 08:10 PM
          0 responses
          11 views
          0 likes
          Last Post ntbone
          by ntbone
           
          Started by chrislou, 08-28-2015, 03:27 AM
          14 responses
          2,883 views
          0 likes
          Last Post nkhoi
          by nkhoi
           
          Working...
          X