How do I resolve the type conversion error in below scenario
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
add a comment |
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
4
You can't increment an integer by0.5. An integer can only represent whole numbers. Why don't you wantideclared as a floating-point type?
– Amy
Nov 21 '18 at 21:33
You are asking how to increment anintegertype by0.5. That now how integers work.
– ja72
Nov 21 '18 at 21:41
If you need a decimal point number, your primary choices are eitherfloatordouble.
– Code-Apprentice
Nov 21 '18 at 21:44
Or ... decimal?
– Captain Wibble
Nov 21 '18 at 21:44
add a comment |
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
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
c# implicit-conversion
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 by0.5. An integer can only represent whole numbers. Why don't you wantideclared as a floating-point type?
– Amy
Nov 21 '18 at 21:33
You are asking how to increment anintegertype by0.5. That now how integers work.
– ja72
Nov 21 '18 at 21:41
If you need a decimal point number, your primary choices are eitherfloatordouble.
– Code-Apprentice
Nov 21 '18 at 21:44
Or ... decimal?
– Captain Wibble
Nov 21 '18 at 21:44
add a comment |
4
You can't increment an integer by0.5. An integer can only represent whole numbers. Why don't you wantideclared as a floating-point type?
– Amy
Nov 21 '18 at 21:33
You are asking how to increment anintegertype by0.5. That now how integers work.
– ja72
Nov 21 '18 at 21:41
If you need a decimal point number, your primary choices are eitherfloatordouble.
– 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
add a comment |
3 Answers
3
active
oldest
votes
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:
- You can change
minHourto be a double, which will solve this problem. - You can cast
minHouras a double like so:var i = (double)minHour
- You can do what you probably should and specify it when you declare
ias so:double i = minHour
I hope this helps!
add a comment |
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)
orfloatinstead ofdouble.
– ja72
Nov 21 '18 at 21:40
Ah, yes, missed the part about theI do not want to declare i as of type double, makes me wonder if OP would likefloat.
– Stefan
Nov 21 '18 at 21:42
1
Decimal is also available, see here
– Captain Wibble
Nov 21 '18 at 21:43
... perhaps evenbytes ;-)
– Stefan
Nov 21 '18 at 21:45
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- You can change
minHourto be a double, which will solve this problem. - You can cast
minHouras a double like so:var i = (double)minHour
- You can do what you probably should and specify it when you declare
ias so:double i = minHour
I hope this helps!
add a comment |
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:
- You can change
minHourto be a double, which will solve this problem. - You can cast
minHouras a double like so:var i = (double)minHour
- You can do what you probably should and specify it when you declare
ias so:double i = minHour
I hope this helps!
add a comment |
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:
- You can change
minHourto be a double, which will solve this problem. - You can cast
minHouras a double like so:var i = (double)minHour
- You can do what you probably should and specify it when you declare
ias so:double i = minHour
I hope this helps!
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:
- You can change
minHourto be a double, which will solve this problem. - You can cast
minHouras a double like so:var i = (double)minHour
- You can do what you probably should and specify it when you declare
ias so:double i = minHour
I hope this helps!
answered Nov 21 '18 at 21:44
Shawn LehnerShawn Lehner
1,260712
1,260712
add a comment |
add a comment |
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)
orfloatinstead ofdouble.
– ja72
Nov 21 '18 at 21:40
Ah, yes, missed the part about theI do not want to declare i as of type double, makes me wonder if OP would likefloat.
– Stefan
Nov 21 '18 at 21:42
1
Decimal is also available, see here
– Captain Wibble
Nov 21 '18 at 21:43
... perhaps evenbytes ;-)
– Stefan
Nov 21 '18 at 21:45
add a comment |
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)
orfloatinstead ofdouble.
– ja72
Nov 21 '18 at 21:40
Ah, yes, missed the part about theI do not want to declare i as of type double, makes me wonder if OP would likefloat.
– Stefan
Nov 21 '18 at 21:42
1
Decimal is also available, see here
– Captain Wibble
Nov 21 '18 at 21:43
... perhaps evenbytes ;-)
– Stefan
Nov 21 '18 at 21:45
add a comment |
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)
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)
edited Nov 21 '18 at 21:48
answered Nov 21 '18 at 21:38
StefanStefan
8,35373760
8,35373760
orfloatinstead ofdouble.
– ja72
Nov 21 '18 at 21:40
Ah, yes, missed the part about theI do not want to declare i as of type double, makes me wonder if OP would likefloat.
– Stefan
Nov 21 '18 at 21:42
1
Decimal is also available, see here
– Captain Wibble
Nov 21 '18 at 21:43
... perhaps evenbytes ;-)
– Stefan
Nov 21 '18 at 21:45
add a comment |
orfloatinstead ofdouble.
– ja72
Nov 21 '18 at 21:40
Ah, yes, missed the part about theI do not want to declare i as of type double, makes me wonder if OP would likefloat.
– Stefan
Nov 21 '18 at 21:42
1
Decimal is also available, see here
– Captain Wibble
Nov 21 '18 at 21:43
... perhaps evenbytes ;-)
– 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 21:49
RichardissimoRichardissimo
4,1912726
4,1912726
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
You can't increment an integer by
0.5. An integer can only represent whole numbers. Why don't you wantideclared as a floating-point type?– Amy
Nov 21 '18 at 21:33
You are asking how to increment an
integertype by0.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
floatordouble.– Code-Apprentice
Nov 21 '18 at 21:44
Or ... decimal?
– Captain Wibble
Nov 21 '18 at 21:44