Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidatorBase - Min/Max Value Data Types #25

Closed
cuppm opened this issue Nov 30, 2023 · 0 comments
Closed

ValidatorBase - Min/Max Value Data Types #25

cuppm opened this issue Nov 30, 2023 · 0 comments

Comments

@cuppm
Copy link

cuppm commented Nov 30, 2023

Is there a clean/efficient way to add type checking to the min/max check in ValidatorBase.cs? Currently if your min/max value is a different type from your editor's value type, it throws an exception that gets caught and hidden in ValidatorBase.IsValidValue(). So the user ends up in a loop of being unable to exit the cell - either by modifying the value or pressing ESC.

Simple example code that when you start an edit of the first cell, you can never exit edit mode for the cell:

static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Form oForm = new Form() {
        StartPosition = FormStartPosition.CenterScreen,
        Text = "SourceGrid Bug Testing",
    };
    SourceGrid.Grid oGrid = new SourceGrid.Grid() {
        Dock = DockStyle.Fill,
    };
    oForm.Controls.Add(oGrid);

    oGrid.Redim(1, 2);
    oGrid[0, 0] = new SourceGrid.Cells.Cell(1.1, typeof(double?)) {
        Editor = new SourceGrid.Cells.Editors.TextBoxNumeric(typeof(double)) {
            AllowNull = true,
            MinimumValue = 0,
        },
    };
    oGrid[0, 1] = new SourceGrid.Cells.Cell(null, typeof(int?)) {
        Editor = new SourceGrid.Cells.Editors.TextBoxNumeric(typeof(int)) {
            AllowNull = true,
            MinimumValue = 0,
        },
    };

    Application.Run(oForm);
}

This is because ValidatorBase throws a System.ArgumentException: 'Object must be of type Int32.' from the CompareTo() because the l_Min and p_Value are different types (int and double):

if (m_MinimumValue != null)
{
	IComparable l_Min = (IComparable)m_MinimumValue;
	if (l_Min.CompareTo(p_Value) > 0)
		return false;
}

In my example changing to MinimumValue = 0.0 for the first cell solves the issue.

The simple solution is to just always use the same type for the min/max as the value type. But for the case of doubles or non-32 bit integers there's the chance that it could slip through like this example, and which the current behavior of silently eating the exception it makes it difficult to figure out why it's happening without digging into the source code.

Perhaps a check on the min/max value variable's type to see if they're convertible to the value type before calling the CompareTo() function? Or maybe a check in the property setters? Or is there a way to modify the code to allow the exception or reason why it's not valid to be seen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants