I am having a issue trying to make a numericUpAndDown into a int. This is the code I have so far. private int counter = numericUpAndDown1.Value;
All help is appreciated, thanks!

- 7
-
2**"i am having an issue"** is not a problem description. you'll have to be more specific than that. – Banana Sep 18 '16 at 17:00
-
And although your "issue" is rather obvious, i will refrain from giving you the answer until you write in your question what the issue is exactly. In many cases, you can solve the issue on your own once you actually read the error message, and it will be much more effective than getting a straight forward answer on a Q&A website. – Banana Sep 18 '16 at 17:08
-
2There's also an elephant in the room here. The keyword *private*. – MattDavey Sep 18 '16 at 17:24
-
Possible duplicate of [How do I convert a decimal to an int in C#?](http://stackoverflow.com/questions/501090/how-do-i-convert-a-decimal-to-an-int-in-c) – rene Sep 18 '16 at 20:05
2 Answers
numericUpAndDown1.Value
is of decimal
type and thus you can't directly store it to INT
and need a explicit cast
private int counter = (int)numericUpAndDown1.Value;

- 76,197
- 13
- 71
- 125
-
-
-
2[msdn.microsoft.com](https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.value(v=vs.110).aspx) – Banana Sep 18 '16 at 17:16
-
OP didn't provide enough information to assume that the value is in fact a decimal type. – dperish Sep 18 '16 at 17:16
-
2@dperish, check the MSDN document link in answer and then come back for argument – Rahul Sep 18 '16 at 17:17
-
-
@dperish it's pretty obvious to everyone else that it is a NumericUpDown control, however I do expect it to be named `numericUpDown1` by default so the `And` does seem a tiny bit suspicious. – Slai Sep 18 '16 at 17:24
-
I understand the assumption. But, could that direct cast to int throw an exception even if the source type is a decimal, or double for that matter? – dperish Sep 18 '16 at 17:32
-
FYI, I checked some documentation. Its actually a double: http://docs.telerik.com/devtools/wpf/controls/radnumericupdown/overview – dperish Sep 18 '16 at 17:33
-
@dperish **"cannot directly cast XXX to YYY"** error in visual studio in each and every case like this one will tell you the type. also, please dont trust information from telerik website over msdn on something belonging to microsoft... – Banana Sep 18 '16 at 17:35
-
a) msft isn't the only game in town b) direct cast from decimal to int may result in an exception: decimal x = 3000000000; int y = (int)x; So what happened there? – dperish Sep 18 '16 at 17:41
-
1@dperish while ms is not the only game in town, their info on c# will always be more correct, you know, since microsoft actually designed and developed c#... and regarding the exception - i agree it might not be ideal, but it can be handled with a simple try/catch block – Banana Sep 18 '16 at 17:48
-
You win. C# is made by Microsoft. Their documentation is the source of truth. Unsafe code/assumptions be dammed. – dperish Sep 18 '16 at 17:52
Without knowning what the type of numericUpAndDown1.Value property is, you could accomplish this with int.Parse as a quick & dirty solution:
private int counter = int.Parse(numericUpAndDown1.Value.ToString());
As suggested in Rahul's answer, you can also try a direct cast in the case where numericUpAndDown1.Value is another numeric type. This can however result in run-time exceptions when the source value is outside of the acceptable range of integer values (less/greater than 2,147,483,647).
private int counter = (int)numericUpAndDown1.Value;
Since both of these can thrown an exception, you could use the int.TryParse method for safety:
private int counter = 0;
int.TryParse(numericUpAndDown1.Value.ToString(), out counter);
If you can post more code to provide some context, then there may be a better suggestion.
Edit:
The following app demonstrates that a direct-cast from decimal to int will in this case throw an exception:
using System;
namespace DecimalToIntTest {
class Program {
static void Main(string[] args) {
decimal x = 3000000000;
int y = (int)x;
Console.WriteLine(y);
Console.Read();
}
}
}

- 1,493
- 16
- 27
-
*This can however result in run-time exceptions when the source value is outside of the acceptable range of integer values...* NO it will not ... rather the bits will be truncated (OR) the extra data will be truncated. Refer MSDN on this. – Rahul Sep 18 '16 at 17:56
-
An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll Additional information: Value was either too large or too small for an Int32. – dperish Sep 18 '16 at 18:06
-
Yeah and that would possible when you use the extrim values `MaxValue` and `MinValue` – Rahul Sep 18 '16 at 18:09
-