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

global namespace error already contains defination

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

    global namespace error already contains defination

    I used the "save as" and copied and indicator.. The copy wont compile,, I attached the error and the part the code that is probable causing the error.. How do I fix this?
    Attached Files

    #2
    Been there, done that. The problem is the enum. This is the global namespace pollution issue -- best answer is "Don't Do That!" You have no business putting names in the global namespace. It is wrong.

    Solution: wrap the enum in a unique namespace, perhaps the same name as the indicator. Add a "using" line for the new namespace, so that the Magic Code at the end will be able to find the enum.

    That should take care of you.

    --EV

    Comment


      #3
      Thanks for helping out here, EV. Yes, issue is likely with the scope of your enum.

      You only need enums declared once per namespace and it will still be available from the copied, new indicator. You should be able to delete the enum declaration completely and it will compile.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_RyanM View Post
        Thanks for helping out here, EV. Yes, issue is likely with the scope of your enum.

        You only need enums declared once per namespace and it will still be available from the copied, new indicator. You should be able to delete the enum declaration completely and it will compile.
        Technically, that is correct. However, I have some problems with just deleting one of them
        • It is bad practice to put names in the global namespace to begin with. Just quit doing that! Wrap them in a namespace. That's what namespaces are for.

        • Since they are in separate indicators, he will be up the proverbial creek if someone wants his indicator that fails to have the definition, but does not want the one that keeps the definition.

        • Keeping just one of them establishes a strong linkage between the two indicators -- which is a Bad Idea. What happens if one of the indicators later finds a need to change the enum?

        Please, folks ... V7 forces enums for parameters up to a high level. Please wrap them in a namespace, rather than letting them pollute the global namespace. That pollution is just plain bad software engineering.

        Example: his first enum is MovingAverageType -- hardly a unique name. What happens if I make one of the same -- it will build fine for me, since I do not have his indicator installed. A problem for anyone who wants to install both of our indicators, however. Please, just use namespaces and put nothing in the global namespace.

        --EV
        Last edited by ETFVoyageur; 01-17-2011, 02:18 PM.

        Comment


          #5
          I didn't create the original indicators I using and modifying.. Can u give me a quick example as to how to wrap them,, maybe a sample code like the ones ninja has to demonstrate various things.. I am interested in exporting my code to some buddies, and seems like this is an issue if we all go to the same code thats available in the public domain

          Comment


            #6
            Originally posted by gg80108 View Post
            I didn't create the original indicators I using and modifying.. Can u give me a quick example as to how to wrap them,, maybe a sample code like the ones ninja has to demonstrate various things.. I am interested in exporting my code to some buddies, and seems like this is an issue if we all go to the same code thats available in the public domain
            Sure -- here is some demo code I use as a model

            --EV

            Code:
            #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Gui.Chart;
            
                using RwbDemoModel;
            #endregion
            
            namespace RwbDemoModel {
                // This namespace is to prevent polluting the global namespace
                public enum DemoEnum {value1, value2}
            }

            Comment


              #7
              Originally posted by gg80108 View Post
              I used the "save as" and copied and indicator.. The copy wont compile,, I attached the error and the part the code that is probable causing the error.. How do I fix this?
              Separate the enum into a unique namespace.

              Example:

              Code:
              // NT standard "Using Declarations section is here
              using _nsDataBoxTest;
              
              namespace _nsDataBoxTest
              {
                  public enum MovAvgType
                  {
                      EMA,
                      SMA,
                      HMA,
                      None
                  }
              }
              
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              
              // rest of the code goes here

              Comment


                #8
                If this is in the indicator,, if I want to call this indicator in a strategy,, I have to add
                using _nsDataBoxTest;
                to the
                Using Declarations section in my strategy also?

                Comment


                  #9
                  Originally posted by gg80108 View Post
                  If this is in the indicator,, if I want to call this indicator in a strategy,, I have to add
                  using _nsDataBoxTest;
                  to the
                  Using Declarations section in my strategy also?
                  Technically, if the Strategy is calling the indicator, then the indicator knows its NameSpace requirements, and should just use them. However, I do not remember testing this directly, so you may just have to try it and see. I am pretty sure I have done it in a Strategy test; I just cannot remember, as my trading methods are now pretty set, and will not change until they stop working.

                  Comment


                    #10
                    Enum vs Namespace

                    So if I understand correctly, if I place my Enums in a namespace they will only work in that indicator and not others that use the same enum? ie they are not global?

                    So the MAType example would only be available to that script and not others?

                    Comment


                      #11
                      I found that these issues show up if u do the "save as" on an indicator with enums/namespace and then try to compile.. For us non geeks not sure why NT doesn't take care of this when it allows u to " save as", guess NT thought that just changing the strategy name would be good enough?

                      I did find that if I do a "save as" that has some enums wrapped in a namespace,, that I have to change the namespace name in the copy,, which is better then having to alter all the enum names if there are several enums..
                      Also found that I have to add the namespace in the declaration using of a strategy if I want to use the indicator in a strategy

                      Comment


                        #12
                        As to the saving -- it actually works quite well

                        There are two cases -- either (a) you do not want the namespace changed when you do Save As ... , such as when you want to use the enum across several of your own indicators or (b) you do want the namespace changed, such as when you want the enum unique to that indicator.

                        In the case of (b) which is what you seemed to want -- just follow my convention. I name the namespace the same as my indicator. Then when Save As ... does its text substitution it updates the namespace name as well. This convention is pretty well guaranteed to not collide, since you cannot have two indicators of the same name anyway.

                        In the case of (a) pick any namespace name you like that is unlikely to get picked by someone else, and not the same as your indicator name. Then Save As ... will not touch it.

                        Put another way, here is my suggestion:
                        • If you have things to share between scripts of yours, (enums, classes, struct definitions, static library functions, etc) put them in a namespace that is unique to you, but does not match any script name. Save As... will not bother this namespace name.

                        • If you have things that are unique to a single script (e.g. enum properties) , put them in a namespace whose name matches the script name. That is pretty well guaranteed to not collide with anything else, including your own other scripts, because the script itself must be a unique name. By making the namespace name and the script name match, Save As... will update both.


                        --EV
                        Last edited by ETFVoyageur; 01-26-2011, 10:40 AM.

                        Comment


                          #13
                          Enclosed is my save as results,, less then trouble free.. Am I doing something wrong setting this up?
                          Think ur wisdom just dawned on me "I name the namespace the same as my indicator. Then when Save As ... does its text substitution it updates the namespace name as well."
                          Attached Files
                          Last edited by gg80108; 01-26-2011, 10:50 AM.

                          Comment


                            #14
                            See my previous posting.

                            In your case you have two indicators with the same namespace declaring the same enums. You can only declare the same enum once per namespace.

                            By the way, please try to pick a namespace name that is likely to be unique to you. Something like "Avgtype" is too likely to collide with one in use by some other indicator that a user may have also installed.

                            As I suggested in my last post, I suggest using the indicator name as the namespace name too.

                            --EV

                            Comment


                              #15
                              Updating this thread here as we have lately seen issues following this approach. If you export as an assembly, your enums declared in their own namespace may not import correctly on another machine. Official recommendation on enum placement is shown in this sample:
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by proptrade13, Today, 11:06 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              4 responses
                              32 views
                              0 likes
                              Last Post quantismo  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              32 views
                              0 likes
                              Last Post love2code2trade  
                              Started by cls71, Today, 04:45 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post eDanny
                              by eDanny
                               
                              Started by kulwinder73, Today, 10:31 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X