5
votes

I'm having a problem passing a numericupdown value into an integer.

private void button5_Click(object sender, EventArgs e)
{
    int count = numericUpDown1.Value;
    updateCount(count);
}

So, what I want to do is pass the value of the numericupdown value into an integer named count and then pass the value into a method that updates a table in my database.

I just started programming C# lately and can't seem to understand some documentation.

4
What is the error message or issue you face on assigning the value to intMohit Shrivastava
Hi, I added a screenshot of it. I'm really sorry for the question. I know it maybe is simple112358d

4 Answers

9
votes

NumericUpDown.Value returns decimal so you need to round and convert to integer

Use this,

int count = Convert.ToInt32(Math.Round(numericUpDown1.Value, 0));
updateCount(count);

You can convert directly to the integer type if you have set Increment to integer (no decimal points) number, otherwise it is safe to round first before conversion.

So without round

int count = Convert.ToInt32(numericUpDown1.Value);
updateCount(count);
0
votes

The issue is that the numeric value of the NumericUpDown control is decimal and you wanted to assign it to a integer type. You should do a TypeCast to assign it. For the same there is a Convert class you can use it like this

int count = Convert.ToInt32(numericUpDown1.Value);
-1
votes

This is what it to do:

Dim numericUpDown1 As Integer
-2
votes

int count = (int)numericUpDown1.Value;