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

Compile this ...

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

    Compile this ...

    Greetings,
    Why does this compile,
    ' if (highflag = false && 1 < -1) '

    and this doesn't ?
    ' if (highflag = false) '
    Bruce

    #2
    Neither should compile it should be:

    if (highflag == false)
    or
    if (!highflag)

    your 1 < -1 will never evaluate to true though

    VT

    Comment


      #3
      I'll check it ' if (highflag = false && 1 < -1) ' again tomorrow, but it compiled today. It's in a Method in an Indicator, fwiw. The ' 1 < -1 ' was a simplification of some code, when I took the ' 1 < -1 ' out, THEN it didn't compile.
      Bruce

      Comment


        #4
        BGWTEX,

        It will depend on your compiler, some might let that pass with only an error warning (Visual Studio for example), However NT's editor will produce an error.

        The reason is, "=" is the assignment operator, and "==" is the equality operator.

        Using:
        if (highflag = false)
        You are assigning highflag the value of false, which will always be a true condition.
        You are not checking if the current value of highflag is true or false, to do that you need to use the "==" equality operator.

        I hope that clarifies things.

        VT

        Comment


          #5
          OK, I'm all clarified, except ... since I'm doing this in NinjaTrader, am I not using NT's editor ? (Where's it's compiling with no errors, after checking it again today.)
          Bruce

          Comment


            #6
            Bruce,

            I have attached 2 screenshots of the snippet of code in OnBareUpdate() in the NT editor.

            This compiles:
            Code:
            [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] OnBarUpdate()[/COLOR][/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]    bool[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] highflag = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]    if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (highflag == [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]    {[/FONT][/SIZE]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]         //Do something [/COLOR][/SIZE][/FONT]
            [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]    } [/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [/SIZE][/FONT]
            This produces an error like you can see in the screenshot:
            Code:
            [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] OnBarUpdate()[/COLOR][/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]    bool[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] highflag = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]    if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (highflag = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]    {[/FONT][/SIZE]
            [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]         //Do something [/COLOR][/SIZE][/FONT]
            [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]    } [/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [/SIZE][/FONT]
            I did notice that the text you put in your post was green. I'm not sure if you colored it in the forum, or whether it was pasted in and is green in the editor. If it is green in the editor, then it is probably commented out code, and the compiler is ignoring it. If you could paste a larger section of the code, I would be happy to take a look at it, and see what might be happening.

            VT
            Attached Files

            Comment


              #7
              Yup, same results I got,
              if (highflag == false) compiles,
              if (highflag = false) gives an error,

              but if (highflag = false && 1 < -1) also compiles.
              It may have been commented out when I copy/pasted, but it was the pretty black & blue like yours is when I compiled it. (Good eye, tho!)
              I'd send you a screenshot, but I've already shut down for the night. Besides, it's not something I need resolved, I thought it might be something NT would want to know about, if it really is a compiled bug.
              Thanks for your explanation, I do appreciate it !!
              Bruce, NT Lifer

              Comment


                #8
                Bruce,

                Interesting, I just tried again with another condition:
                if (highflag = false && lowflag < 5)
                And it does compile. However, lowflag = 5 does not, but lowflag == 5 does.
                Having lowflag as a second condition and a bool, lowflag = true doesn't compile but this will:
                if (highflag = false && lowflag == true)

                That is very strange.
                VT

                Comment


                  #9
                  Bruce or VT could you guys please attach the cs for the code that would not expect to compile so I can look into it here?

                  Thanks much,
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Bertrand,

                    I posted a screenshot with some sample test code from VS and you can see from the squiggles and errors which ones it didn't like. I also included the sample test code that compiles "as is", although the conditional statements using "=" would produce unexpected results.

                    I didn't really have any "expectations" other than just to be surprised "highflag = true" would compile under any circumstance. It certainly is not a problem with NT's editor in my opinion. I just found it interesting that it can be used that way only when combined with another valid conditional statement.

                    Personally I would prefer if neither compiler would let "highflag = true" inside an "if" statement compile under any circumstance. At best, using an assignment inside a conditional statement would be a confusing coding style.

                    VT

                    edit: One last test:
                    if (highflag = true && highint == 3) { } Compiles
                    but if order reversed:
                    if (highint == 3 && highflag = true) { } Doesn't compile
                    Attached Files
                    Last edited by VTtrader; 09-22-2011, 08:16 AM.

                    Comment


                      #11
                      Hello VTrader,

                      Thanks for the script and I was able to confirm your results on my end.

                      There appears to be a special case when using a combination of bool checks with && || operators.

                      We use the standard C# Compiler and your NS code will be accepted to what this compiler was designed to pass.
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        Matthew,

                        Believe me that's not how I would code it!
                        I would consider it very bad style to try to. I was in fact just very surprised any compiler would allow it in the first place. To me it would make it very hard to track down a bug if you inadvertantly used "=" and have it compile.

                        VT

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by hurleydood, 09-12-2019, 10:45 AM
                        14 responses
                        1,096 views
                        0 likes
                        Last Post Board game geek  
                        Started by cre8able, Yesterday, 04:16 PM
                        1 response
                        16 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by cre8able, Yesterday, 04:22 PM
                        1 response
                        14 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by stafe, 04-15-2024, 08:34 PM
                        5 responses
                        28 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by StrongLikeBull, Yesterday, 04:05 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X