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 Tim-c, Today, 03:54 AM
          0 responses
          3 views
          0 likes
          Last Post Tim-c
          by Tim-c
           
          Started by FrancisMorro, Today, 03:24 AM
          0 responses
          2 views
          0 likes
          Last Post FrancisMorro  
          Started by Segwin, 05-07-2018, 02:15 PM
          10 responses
          1,770 views
          0 likes
          Last Post Leafcutter  
          Started by Rapine Heihei, 04-23-2024, 07:51 PM
          2 responses
          31 views
          0 likes
          Last Post Max238
          by Max238
           
          Started by Shansen, 08-30-2019, 10:18 PM
          24 responses
          945 views
          0 likes
          Last Post spwizard  
          Working...
          X