chesta
chesta

Reputation: 315

strange ArgumentOutOfRangeException in NumericUpDown

In my current project, I am using NumericUpDowns to display some values. Everything's fine, but there's one line of code that throws an ArgumentOutOfRangeException, which is the third one of the following:

verBox.SelectedIndex = (int)currentConfig.Version; //enum, casting to int
startIndexBox.Value = currentConfig.StartIndex + 1;
intervalBox.Value = currentConfig.Interval;

so verBox is a ComboBox, startIndexBox and intervalBox are NumericUpDowns. When this code is executed, the fields of currentConfig have following values:

currentConfig.Version = 2
currentConfig.StartIndex = 0
currentConfig.Interval = 700

Now the strange thing is the exception (thrown in the third line of this code): It says that the Value 2 is invalid, and that it should be between Minimum and Maximum.
First thing is, that the value is not 2, but 700.
And the second thing is, that intervalBox.Minimum is 0, and the Maximum is 10k.

Also, Visual Studio doesn't break at this exception, but the program ends with Code -1073741819 (0xc0000005) 'Access violation'. If I set all exceptions to be thrown in the debugging settings, it breaks and I can see that currentConfig.Interval is 700, and there is in fact no reason for this exception to be thrown. Another strange thing I observed is, that if I surround the last line of this code with a try/catch, the same exception is thrown on the line above it.

Did someone experience anything like this before? Or any ideas on how to solve this?

Edit: If I set a breakpoint on the line that throws the exception, the exception is thrown anyways.

Upvotes: 2

Views: 889

Answers (2)

chesta
chesta

Reputation: 315

Okay I solved the problem now: Visual Studio marked the exception on the wrong line. The problem was, that startIndexBox.Value got an index which was indeed out of range, causing the exception to occur.

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31656

Verify that currentConfig is not null by an if check. If it is not null, verify that the properties of Version, StartiIndex and Interval contain values which are expected and proper for the numeric controls.

Also verify that the numeric controls are valid and on the page and ready to accept values at the point you want to load them.

Upvotes: 1

Related Questions