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

Casting a Variable inside a loop... what is variable type next time loop runs

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

    Casting a Variable inside a loop... what is variable type next time loop runs

    Math in C# ties me in knots some times. I need someInt to be an int so its the correct type for use as an argument.

    I need someDouble to be a double because someOtherDouble is a percentage ( like .025)

    Code:
    while (recordIn != null) 
        {
           someDouble = Math.Round(someDouble * someOtherDouble);
           someInt = (int) someDouble;
        }
    I'm sure the above will work on the first run of this while loop.... but I don't know how long that cast of someDouble to a int lasts.

    What type is someDouble on the SECOND run of he loop? Does the cast to int persist and it is now a int forever.... or does it revert back to its declared type as double... I declared it as private double in the variables section of a strategy....

    Teaching myself as I go along so bear with me please

    #2
    If everything is as you describe, I think it should remain as an int.

    Comment


      #3
      What about promotion?

      I just thought of something....

      What about automatic promotion..... if the cast persists, on the second run of the loop, during the multiplication....

      Code:
      someDouble = Math.Round(someDouble * someOtherDouble);
      won't someDouble get automatically promoted to a double again.... and will then be all set to get cast back to an int on the next line

      Code:
      someInt = (int) someDouble;
      Then the loop will run again and it get promoted then cast again....

      Can someone please confirm?

      Comment


        #4
        I'm not sure what you're asking, but with the line

        someInt = (int)someDouble;

        you are storing someDouble as an int in the variable 'someInt', you aren't modifying someDouble.

        Edit: also, this post should answer your original question in this thread. Disregard my first post in this thread, I misread what you were asking.
        Last edited by Radical; 04-12-2012, 07:01 PM.

        Comment


          #5
          Originally posted by Radical View Post
          I'm not sure what you're asking, but with the line

          someInt = (int)someDouble;

          you are storing someDouble as an int in the variable 'someInt', you aren't modifying someDouble.

          Ahh... yes that's the key to it... I needed to know if I was modifying someDouble into an int.... I was thinking maybe I was changing the type of someDouble from a double as declared to now be a int.

          But I think I understand now... I am not changing someDouble's type... I am changing the value stored inside someDouble to an int and then storing it into someInt... leaving someDouble as type double.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
          55 responses
          5,454 views
          0 likes
          Last Post BartMan
          by BartMan
           
          Started by DawnTreader, 05-08-2024, 05:58 PM
          16 responses
          52 views
          0 likes
          Last Post DawnTreader  
          Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
          15 responses
          61 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by kevinenergy, Yesterday, 12:01 PM
          8 responses
          28 views
          0 likes
          Last Post kevinenergy  
          Started by nightstalker, Today, 01:32 PM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Working...
          X