Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          574 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          332 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X