How do I resolve the type conversion error in below scenario












-1















I am going through basics of C# and .NET programming. I have a for loop as shown below.



for(var i = minHour; i <= maxHour; i++)


When i replace the above as:



for(var i = minHour; i <= maxHour; i+=0.5)


then i see an error as:




Cannot implicitly convert type double to int.




I do not want to declare i as of type double, please help. I do not know anything about implicit conversion.



Can someone please help me how do I correct this,thanks in advance.










share|improve this question




















  • 4





    You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

    – Amy
    Nov 21 '18 at 21:33











  • You are asking how to increment an integer type by 0.5. That now how integers work.

    – ja72
    Nov 21 '18 at 21:41











  • If you need a decimal point number, your primary choices are either float or double.

    – Code-Apprentice
    Nov 21 '18 at 21:44











  • Or ... decimal?

    – Captain Wibble
    Nov 21 '18 at 21:44
















-1















I am going through basics of C# and .NET programming. I have a for loop as shown below.



for(var i = minHour; i <= maxHour; i++)


When i replace the above as:



for(var i = minHour; i <= maxHour; i+=0.5)


then i see an error as:




Cannot implicitly convert type double to int.




I do not want to declare i as of type double, please help. I do not know anything about implicit conversion.



Can someone please help me how do I correct this,thanks in advance.










share|improve this question




















  • 4





    You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

    – Amy
    Nov 21 '18 at 21:33











  • You are asking how to increment an integer type by 0.5. That now how integers work.

    – ja72
    Nov 21 '18 at 21:41











  • If you need a decimal point number, your primary choices are either float or double.

    – Code-Apprentice
    Nov 21 '18 at 21:44











  • Or ... decimal?

    – Captain Wibble
    Nov 21 '18 at 21:44














-1












-1








-1








I am going through basics of C# and .NET programming. I have a for loop as shown below.



for(var i = minHour; i <= maxHour; i++)


When i replace the above as:



for(var i = minHour; i <= maxHour; i+=0.5)


then i see an error as:




Cannot implicitly convert type double to int.




I do not want to declare i as of type double, please help. I do not know anything about implicit conversion.



Can someone please help me how do I correct this,thanks in advance.










share|improve this question
















I am going through basics of C# and .NET programming. I have a for loop as shown below.



for(var i = minHour; i <= maxHour; i++)


When i replace the above as:



for(var i = minHour; i <= maxHour; i+=0.5)


then i see an error as:




Cannot implicitly convert type double to int.




I do not want to declare i as of type double, please help. I do not know anything about implicit conversion.



Can someone please help me how do I correct this,thanks in advance.







c# implicit-conversion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:32









Amy

21.6k1874131




21.6k1874131










asked Nov 21 '18 at 21:30









mdevmmdevm

127111




127111








  • 4





    You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

    – Amy
    Nov 21 '18 at 21:33











  • You are asking how to increment an integer type by 0.5. That now how integers work.

    – ja72
    Nov 21 '18 at 21:41











  • If you need a decimal point number, your primary choices are either float or double.

    – Code-Apprentice
    Nov 21 '18 at 21:44











  • Or ... decimal?

    – Captain Wibble
    Nov 21 '18 at 21:44














  • 4





    You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

    – Amy
    Nov 21 '18 at 21:33











  • You are asking how to increment an integer type by 0.5. That now how integers work.

    – ja72
    Nov 21 '18 at 21:41











  • If you need a decimal point number, your primary choices are either float or double.

    – Code-Apprentice
    Nov 21 '18 at 21:44











  • Or ... decimal?

    – Captain Wibble
    Nov 21 '18 at 21:44








4




4





You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

– Amy
Nov 21 '18 at 21:33





You can't increment an integer by 0.5. An integer can only represent whole numbers. Why don't you want i declared as a floating-point type?

– Amy
Nov 21 '18 at 21:33













You are asking how to increment an integer type by 0.5. That now how integers work.

– ja72
Nov 21 '18 at 21:41





You are asking how to increment an integer type by 0.5. That now how integers work.

– ja72
Nov 21 '18 at 21:41













If you need a decimal point number, your primary choices are either float or double.

– Code-Apprentice
Nov 21 '18 at 21:44





If you need a decimal point number, your primary choices are either float or double.

– Code-Apprentice
Nov 21 '18 at 21:44













Or ... decimal?

– Captain Wibble
Nov 21 '18 at 21:44





Or ... decimal?

– Captain Wibble
Nov 21 '18 at 21:44












3 Answers
3






active

oldest

votes


















1














I am not sure I follow why you want to keep using var for this situation instead of simply defining double i = which would solve your problem, but because you specified it in your question, I will ignore that for now.



Instead, let's take a look at why it is throwing this error in the first place. You are using var i = to define your variable, which you might think simply puts i into a generic variable. This isn't accurate in C#. Instead, C# will attempt to infer the type of this variable and because you are using var i = minHour the type of i is being inferred by the type of minHour.



You have a few options to fix this issue:




  1. You can change minHour to be a double, which will solve this problem.

  2. You can cast minHour as a double like so: var i = (double)minHour

  3. You can do what you probably should and specify it when you declare i as so: double i = minHour


I hope this helps!






share|improve this answer































    1














    You can solve it like this:



    //beware when using i as indexer.
    for(var i = minHour * 2; i <= maxHour * 2; i++)
    //note: both minHour and maxHour need to be multiplied by 2
    //Proof:
    // n = maxHour - minHour =>
    // 2 n = 2 (maxHour - minHour) =>
    // (maxHour - minHour) * 2 = 2 * maxHour - 2 * minHour
    //


    Or, alternatively:



    //type changed to double:
    double minHour = 0, maxHour = 1;
    for(var i = minHour; i <= maxHour; i+=0.5)


    Or better (but not your favorite);



    for(double i = minHour; i <= maxHour; i+=0.5)





    share|improve this answer


























    • or float instead of double.

      – ja72
      Nov 21 '18 at 21:40













    • Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

      – Stefan
      Nov 21 '18 at 21:42






    • 1





      Decimal is also available, see here

      – Captain Wibble
      Nov 21 '18 at 21:43











    • ... perhaps even bytes ;-)

      – Stefan
      Nov 21 '18 at 21:45



















    0














    In the two examples, the variable i is being initialized to minHour. The compiler will infer the type from that statement alone, not any other usage. Presumably minHour is an int, so in both examples i is an int. Since it is an int the error message should now make more sense.



    Note that var isn't some kind of dynamic type, which I suspect is the source of your misunderstanding. It is a known, fixed typed whose type is decided at compile time from its usage. So in both examples they are the equivalent of having the word var replaced by int. If you explicitly declare it as a double it will do what you want.



    Tip: if you want to know what type the var will be, hover over it with the mouse.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420729%2fhow-do-i-resolve-the-type-conversion-error-in-below-scenario%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I am not sure I follow why you want to keep using var for this situation instead of simply defining double i = which would solve your problem, but because you specified it in your question, I will ignore that for now.



      Instead, let's take a look at why it is throwing this error in the first place. You are using var i = to define your variable, which you might think simply puts i into a generic variable. This isn't accurate in C#. Instead, C# will attempt to infer the type of this variable and because you are using var i = minHour the type of i is being inferred by the type of minHour.



      You have a few options to fix this issue:




      1. You can change minHour to be a double, which will solve this problem.

      2. You can cast minHour as a double like so: var i = (double)minHour

      3. You can do what you probably should and specify it when you declare i as so: double i = minHour


      I hope this helps!






      share|improve this answer




























        1














        I am not sure I follow why you want to keep using var for this situation instead of simply defining double i = which would solve your problem, but because you specified it in your question, I will ignore that for now.



        Instead, let's take a look at why it is throwing this error in the first place. You are using var i = to define your variable, which you might think simply puts i into a generic variable. This isn't accurate in C#. Instead, C# will attempt to infer the type of this variable and because you are using var i = minHour the type of i is being inferred by the type of minHour.



        You have a few options to fix this issue:




        1. You can change minHour to be a double, which will solve this problem.

        2. You can cast minHour as a double like so: var i = (double)minHour

        3. You can do what you probably should and specify it when you declare i as so: double i = minHour


        I hope this helps!






        share|improve this answer


























          1












          1








          1







          I am not sure I follow why you want to keep using var for this situation instead of simply defining double i = which would solve your problem, but because you specified it in your question, I will ignore that for now.



          Instead, let's take a look at why it is throwing this error in the first place. You are using var i = to define your variable, which you might think simply puts i into a generic variable. This isn't accurate in C#. Instead, C# will attempt to infer the type of this variable and because you are using var i = minHour the type of i is being inferred by the type of minHour.



          You have a few options to fix this issue:




          1. You can change minHour to be a double, which will solve this problem.

          2. You can cast minHour as a double like so: var i = (double)minHour

          3. You can do what you probably should and specify it when you declare i as so: double i = minHour


          I hope this helps!






          share|improve this answer













          I am not sure I follow why you want to keep using var for this situation instead of simply defining double i = which would solve your problem, but because you specified it in your question, I will ignore that for now.



          Instead, let's take a look at why it is throwing this error in the first place. You are using var i = to define your variable, which you might think simply puts i into a generic variable. This isn't accurate in C#. Instead, C# will attempt to infer the type of this variable and because you are using var i = minHour the type of i is being inferred by the type of minHour.



          You have a few options to fix this issue:




          1. You can change minHour to be a double, which will solve this problem.

          2. You can cast minHour as a double like so: var i = (double)minHour

          3. You can do what you probably should and specify it when you declare i as so: double i = minHour


          I hope this helps!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 21:44









          Shawn LehnerShawn Lehner

          1,260712




          1,260712

























              1














              You can solve it like this:



              //beware when using i as indexer.
              for(var i = minHour * 2; i <= maxHour * 2; i++)
              //note: both minHour and maxHour need to be multiplied by 2
              //Proof:
              // n = maxHour - minHour =>
              // 2 n = 2 (maxHour - minHour) =>
              // (maxHour - minHour) * 2 = 2 * maxHour - 2 * minHour
              //


              Or, alternatively:



              //type changed to double:
              double minHour = 0, maxHour = 1;
              for(var i = minHour; i <= maxHour; i+=0.5)


              Or better (but not your favorite);



              for(double i = minHour; i <= maxHour; i+=0.5)





              share|improve this answer


























              • or float instead of double.

                – ja72
                Nov 21 '18 at 21:40













              • Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

                – Stefan
                Nov 21 '18 at 21:42






              • 1





                Decimal is also available, see here

                – Captain Wibble
                Nov 21 '18 at 21:43











              • ... perhaps even bytes ;-)

                – Stefan
                Nov 21 '18 at 21:45
















              1














              You can solve it like this:



              //beware when using i as indexer.
              for(var i = minHour * 2; i <= maxHour * 2; i++)
              //note: both minHour and maxHour need to be multiplied by 2
              //Proof:
              // n = maxHour - minHour =>
              // 2 n = 2 (maxHour - minHour) =>
              // (maxHour - minHour) * 2 = 2 * maxHour - 2 * minHour
              //


              Or, alternatively:



              //type changed to double:
              double minHour = 0, maxHour = 1;
              for(var i = minHour; i <= maxHour; i+=0.5)


              Or better (but not your favorite);



              for(double i = minHour; i <= maxHour; i+=0.5)





              share|improve this answer


























              • or float instead of double.

                – ja72
                Nov 21 '18 at 21:40













              • Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

                – Stefan
                Nov 21 '18 at 21:42






              • 1





                Decimal is also available, see here

                – Captain Wibble
                Nov 21 '18 at 21:43











              • ... perhaps even bytes ;-)

                – Stefan
                Nov 21 '18 at 21:45














              1












              1








              1







              You can solve it like this:



              //beware when using i as indexer.
              for(var i = minHour * 2; i <= maxHour * 2; i++)
              //note: both minHour and maxHour need to be multiplied by 2
              //Proof:
              // n = maxHour - minHour =>
              // 2 n = 2 (maxHour - minHour) =>
              // (maxHour - minHour) * 2 = 2 * maxHour - 2 * minHour
              //


              Or, alternatively:



              //type changed to double:
              double minHour = 0, maxHour = 1;
              for(var i = minHour; i <= maxHour; i+=0.5)


              Or better (but not your favorite);



              for(double i = minHour; i <= maxHour; i+=0.5)





              share|improve this answer















              You can solve it like this:



              //beware when using i as indexer.
              for(var i = minHour * 2; i <= maxHour * 2; i++)
              //note: both minHour and maxHour need to be multiplied by 2
              //Proof:
              // n = maxHour - minHour =>
              // 2 n = 2 (maxHour - minHour) =>
              // (maxHour - minHour) * 2 = 2 * maxHour - 2 * minHour
              //


              Or, alternatively:



              //type changed to double:
              double minHour = 0, maxHour = 1;
              for(var i = minHour; i <= maxHour; i+=0.5)


              Or better (but not your favorite);



              for(double i = minHour; i <= maxHour; i+=0.5)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 21 '18 at 21:48

























              answered Nov 21 '18 at 21:38









              StefanStefan

              8,35373760




              8,35373760













              • or float instead of double.

                – ja72
                Nov 21 '18 at 21:40













              • Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

                – Stefan
                Nov 21 '18 at 21:42






              • 1





                Decimal is also available, see here

                – Captain Wibble
                Nov 21 '18 at 21:43











              • ... perhaps even bytes ;-)

                – Stefan
                Nov 21 '18 at 21:45



















              • or float instead of double.

                – ja72
                Nov 21 '18 at 21:40













              • Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

                – Stefan
                Nov 21 '18 at 21:42






              • 1





                Decimal is also available, see here

                – Captain Wibble
                Nov 21 '18 at 21:43











              • ... perhaps even bytes ;-)

                – Stefan
                Nov 21 '18 at 21:45

















              or float instead of double.

              – ja72
              Nov 21 '18 at 21:40







              or float instead of double.

              – ja72
              Nov 21 '18 at 21:40















              Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

              – Stefan
              Nov 21 '18 at 21:42





              Ah, yes, missed the part about the I do not want to declare i as of type double, makes me wonder if OP would like float.

              – Stefan
              Nov 21 '18 at 21:42




              1




              1





              Decimal is also available, see here

              – Captain Wibble
              Nov 21 '18 at 21:43





              Decimal is also available, see here

              – Captain Wibble
              Nov 21 '18 at 21:43













              ... perhaps even bytes ;-)

              – Stefan
              Nov 21 '18 at 21:45





              ... perhaps even bytes ;-)

              – Stefan
              Nov 21 '18 at 21:45











              0














              In the two examples, the variable i is being initialized to minHour. The compiler will infer the type from that statement alone, not any other usage. Presumably minHour is an int, so in both examples i is an int. Since it is an int the error message should now make more sense.



              Note that var isn't some kind of dynamic type, which I suspect is the source of your misunderstanding. It is a known, fixed typed whose type is decided at compile time from its usage. So in both examples they are the equivalent of having the word var replaced by int. If you explicitly declare it as a double it will do what you want.



              Tip: if you want to know what type the var will be, hover over it with the mouse.






              share|improve this answer




























                0














                In the two examples, the variable i is being initialized to minHour. The compiler will infer the type from that statement alone, not any other usage. Presumably minHour is an int, so in both examples i is an int. Since it is an int the error message should now make more sense.



                Note that var isn't some kind of dynamic type, which I suspect is the source of your misunderstanding. It is a known, fixed typed whose type is decided at compile time from its usage. So in both examples they are the equivalent of having the word var replaced by int. If you explicitly declare it as a double it will do what you want.



                Tip: if you want to know what type the var will be, hover over it with the mouse.






                share|improve this answer


























                  0












                  0








                  0







                  In the two examples, the variable i is being initialized to minHour. The compiler will infer the type from that statement alone, not any other usage. Presumably minHour is an int, so in both examples i is an int. Since it is an int the error message should now make more sense.



                  Note that var isn't some kind of dynamic type, which I suspect is the source of your misunderstanding. It is a known, fixed typed whose type is decided at compile time from its usage. So in both examples they are the equivalent of having the word var replaced by int. If you explicitly declare it as a double it will do what you want.



                  Tip: if you want to know what type the var will be, hover over it with the mouse.






                  share|improve this answer













                  In the two examples, the variable i is being initialized to minHour. The compiler will infer the type from that statement alone, not any other usage. Presumably minHour is an int, so in both examples i is an int. Since it is an int the error message should now make more sense.



                  Note that var isn't some kind of dynamic type, which I suspect is the source of your misunderstanding. It is a known, fixed typed whose type is decided at compile time from its usage. So in both examples they are the equivalent of having the word var replaced by int. If you explicitly declare it as a double it will do what you want.



                  Tip: if you want to know what type the var will be, hover over it with the mouse.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 21:49









                  RichardissimoRichardissimo

                  4,1912726




                  4,1912726






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420729%2fhow-do-i-resolve-the-type-conversion-error-in-below-scenario%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Tonle Sap (See)

                      I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                      Guatemaltekische Davis-Cup-Mannschaft